jquery插件方法的概念
我刚刚开始制作我自己的 jquery 插件。我仍然没有完全理解使用它们并在插件中添加方法的概念,我最近的尝试失败了,我不知道为什么。在下面的情况下,我得到了用于执行 capFirst 的常规代码,我想将其转换为插件。.
HTML
<div id="contain">
<input type="text" /><br />
<input type="text" /><br />
<input type="text" />
</div>
原始 jquery
function capitaliseFirstLetter(string)
{
return string.charAt(0).toUpperCase() + string.slice(1);
}
$('#contain > input').keyup(function(){
var $this = $(this);
$this.val(capitaliseFirstLetter($this.val()));
});
我的尝试插件代码
(function($) {
$.fn.capFirst = function () {
$(this).keyup(function(){
var $this = $(this);
$this.val(methods.capitaliseFirstLetter($this.val()));
});
};
var methods = function () {
capitaliseFirstLetter: function(inputValue) {
return inputValue.charAt(0).toUpperCase() + inputValue.slice(1);
}
};
})(jQuery);
$('#contain > input').capFirst();
jsfiddle: http://jsfiddle.net/ypWTd/118/
更新
jsfiddle 中的工作解决方案: http://jsfiddle.net/ypWTd/163/
I'm just starting of by making my own jquery plugins. I still havent wrapped my head around the concept of working with them and adding methods in the plugin and my recent attempt is failing and i dont know why. In this case bellow I got my regular piece of code i use to perform a capFirst and i want to transform that to the plugin..
HTML
<div id="contain">
<input type="text" /><br />
<input type="text" /><br />
<input type="text" />
</div>
Original jquery
function capitaliseFirstLetter(string)
{
return string.charAt(0).toUpperCase() + string.slice(1);
}
$('#contain > input').keyup(function(){
var $this = $(this);
$this.val(capitaliseFirstLetter($this.val()));
});
My try of plugin code
(function($) {
$.fn.capFirst = function () {
$(this).keyup(function(){
var $this = $(this);
$this.val(methods.capitaliseFirstLetter($this.val()));
});
};
var methods = function () {
capitaliseFirstLetter: function(inputValue) {
return inputValue.charAt(0).toUpperCase() + inputValue.slice(1);
}
};
})(jQuery);
$('#contain > input').capFirst();
jsfiddle: http://jsfiddle.net/ypWTd/118/
UPDATE
working solution in jsfiddle: http://jsfiddle.net/ypWTd/163/
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
该问题是由以下部分引起的:
这实际上会导致语法错误。您似乎混淆了对象文字所需的语法和“类”所需的语法(需要使用 new 运算符创建其实例)。如果您只是将其设为对象文字,而不是函数,它将起作用:
这是一个 更新的小提琴 。
另请注意,在您的插件方法中,
this
已经是 jQuery 的实例,因此您不需要再次将其传递给 jQuery:Update
另一种方式(使用
function
而不是对象文字)会像这样:这是一个 示例 该版本的。
The problem is caused by the following section:
This actually causes a SyntaxError. You seem to mixing up the syntax between that required for an object literal, and that required for a "class" (an instance of which would need to be created with the
new
operator). If you just make it an object literal, rather than a function, it will work:Here's an updated fiddle.
Also note that in your plugin method,
this
is already an instance of jQuery so you don't need to pass it in to jQuery again:Update
The other way (using a
function
instead of an object literal) would go something like this:Here's an example of that version.
唯一明显的区别是您用于“this”的双 $($())。您是否尝试过删除其中一个 $()?
其次:通常您应该“返回此”以启用链接。
第三:通常您应该通过在插件中使用 .each() 来提供对多个元素选择器的支持
第四:您尝试在不首先使用“new”运算符的情况下访问类的方法。要么你应该这样做
,要么你应该首先实例化对象
The only obvious difference is the double $($()) you are using for "this". Did you ever try to remove one of those $()?
Second: Usually you should "return this" to enable chaining.
Third: Usually you should provide support for multiple element selectors by using .each() inside the plugin
Fourth: You are trying to access a method of a class without using the "new" operator first. Either you should do
or you should instantiate the object first
jQuery 文档包含一些关于插件/Authoring 的有用信息。
您可以在此处找到我的完整编辑。
jQuery documentation contains some good information about Plugins/Authoring.
You´ll find my complete edit here.
这就是您的插件应该如何
查看更新的 jsFiddle
This is how your plugin should be
Check out the updated jsFiddle