不知道我是否直接看到有关包装方法的理论
这是该理论的代码,
$.fn greenify = function(){
return this.css ('color','green');
}
因此可以按如下方式使用
$('#today').greenify();
,该理论说:
“包装器方法必须始终返回原始集合,因此返回此
。这样你可以在链中使用你的方法。”
对我来说,到目前为止我认为包装方法是一种定位多个 HTML 元素的方法。这是否仅仅意味着如果您定位这些元素,您实际上会获得这些目标 HTML 元素,因为它们是未经修改的?因此,当您说 return this 时,您想确认您确切地知道自己得到了什么,以便知道链接时使用的是什么?或者它有什么不同的含义吗?
Here's the code the piece of theory is about
$.fn greenify = function(){
return this.css ('color','green');
}
so this can be used as follows
$('#today').greenify();
then, the theory says:
"A wrapper method must always return the original set, hence the return this
. This way you can use your method in a chain."
To me, a wrapper method so far I saw as a way to target multiple HTML elements. Does it simply mean that if you target those elements, you actually get those targeted HTML elements just as they are, unmodified? So that when you say return this, you want to confirm you know exactly what you're getting so you know what you're using when you're chaining? Or does it mean something different?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
包装方法基本上意味着
在你的情况下你想要
在这里你的方法修改集合中每个 dom 元素的颜色。并且您已经可以断言
this.each
作为 jQuery 对象$(s)
上的方法将返回集合$(s)
所以您的方法还会返回$(s)
。当然,为了简单起见,您的函数会执行相同的操作,因为
$.fn.css
经过优化,可以在后台工作。为了清楚起见,我们自己调用$.fn.each
而不是让$.fn.css
为我们调用它。A wrapper method basically means that
In your case you want
Here your method modifies the colour of every dom element in the set. And you can already assert that
this.each
as a method on a jQuery object$(s)
will return the set$(s)
so your method also returns$(s)
.Of course for simplicity sake your function does the same because
$.fn.css
is optimised to work on a set under the hood. For clarity were calling$.fn.each
ourself rather then having$.fn.css
call it for us.