Jquery 1.6刚刚出来,我从.is
抓取了这段代码API 页面。
我只是对在那里看到的一段代码有几个问题:
1) 为什么是return
需要吗?难道就没有其他更简单的写法吗?我的意思是,我从未使用过 return
当我写过任何 Jquery 的东西时。显然我还没有写任何高级的东西,我仍然在学习。 为什么需要它,什么时候会使用它?还有其他方法可以做同样的事情吗?
2) $('strong', this)
表示“此元素中的任何 <strong>
元素”。 对吗?那整行应该是“给我其中包含 2 个强元素的元素”? 对吗?或者?(我想我错了。)没有其他方法可以写这个吗?我只见过这个使用了几次。
$("li").click(function() {
var $li = $(this),
isWithTwo = $li.is(function() {
return $('strong', this).length === 2;
});
if ( isWithTwo ) {
$li.css("background-color", "green"); } else {
$li.css("background-color", "red"); } });
最佳答案
why is return needed?
看看documentation for is
。重点是该函数将返回 true 或 false。
$('strong', this)
means "any<strong>
elements in this element." right?
是的
and that full line would read "give me the element that has 2 strong elements within this"?
没有。它将是“如果有 2 个强元素,则返回 true,否则返回 false”
关于javascript - Jquery:你能给我解释一下这行代码吗?帮助我理解它,或者提供我可以了解它的引用资料,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5882330/