jQuery迭代问题(被书中的例子搞糊涂了)
我正在研究 O'Reilly jQuery Cookbook。上页。 100 有一个例子,我没有得到任何细节。这是我接触 jQuery 的第一周,所以这并不奇怪,但我希望有人能澄清一下。
该功能是一个带有几个铃铛和按钮的开关。口哨:
onValue
和 offValue
都是布尔值,并且必须具有相反的含义,并且只有其中之一可能会更清楚,但其想法是适应某些地方(例如)name
为“禁用”。 可选的 on
也是一个布尔值,允许调用者将其用作“设置”而不是“切换”
jQuery.fn.toggleAttr = function (name, onValue, offValue, on) {
function set($element, on) {
var value = on ? onValue : offValue;
return value == null ? $element.removeAttr(name) : $element.attr(name, value);
}
return on !== undefined ?
// next line is where I'm confused
set(this, on) :
this.each(function (i, element) {
var $element = $(element);
set($element, $element.attr(name) !== onValue);
});
};
set( this, on )
在这里如何工作?它似乎正在处理元素的列表,但是每个元素都需要发生一些事情,而且我不知道什么会导致任何迭代。我本来期望更像 on === undefined
的情况,比如:
this.each (function( i, element ) {
set( $(element), on);
)}
那么,我错过了什么吗?
I'm working my way through the O'Reilly jQuery Cookbook. On p. 100 there is an example where I don't get one detail. I'm in my first week of looking at jQuery, so that's no surprise, but I'm hoping someone can clarify.
The function is a toggle with a few bells & whistles:
onValue
and offValue
are both Booleans, and must have opposite senses, and it might have been clearer to have only one of them, but the idea is to accommodate something where (for example) name
is "disable".
the optional on
, also a Boolean, lets caller use this as a "set" instead of a "toggle"
jQuery.fn.toggleAttr = function (name, onValue, offValue, on) {
function set($element, on) {
var value = on ? onValue : offValue;
return value == null ? $element.removeAttr(name) : $element.attr(name, value);
}
return on !== undefined ?
// next line is where I'm confused
set(this, on) :
this.each(function (i, element) {
var $element = $(element);
set($element, $element.attr(name) !== onValue);
});
};
How is set( this, on )
working here? It seems to be working on the list of elements, but something needs to happen to each element, and I don't see what would cause any iteration. I'd have expected something more like the on === undefined
case, something like:
this.each (function( i, element ) {
set( $(element), on);
)}
So, am I missing something?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
当未设置
on
时,toggleAttr 函数会循环遍历每个元素,检查name
属性的当前值设置并将其与onValue 进行比较变量。
当设置了
on
时,这意味着您告诉toggleAttr“我希望您将所有元素切换为真/假并忽略当前值”。因此,它不需要循环遍历所有元素并检查当前值的设置,因为您告诉它暴力破解该值。例子:
http://jsfiddle.net/petersendidit/sHJC3/2/
When
on
is not set then the toggleAttr function loops through each of the elements checking what the current value of thename
attribute is set to and comparing that to theonValue
variable.When
on
IS set then that means you are telling toggleAttr "I want you to toggle all elements to true/false and ignore what the current value is". So it doesn't need to loop through all the elements and check what the current value is set to because you told it to brute force the value.Example:
http://jsfiddle.net/petersendidit/sHJC3/2/