你能解释为什么这http://www.sebastiansulinski.co.uk/demos/jquery_show_hide/工程? 所有类都是相同的,但是当您单击打开时,只会打开一个文本。为什么?Jquery。为什么这有效?
0
A
回答
6
它仅打开一个文本容器,因为点击事件绑定到与div.trigger
匹配的每个单独元素。当您点击其中一个匹配元素时,您只需点击一个匹配元素,而不是全部3个。点击回调函数内部的$(this)
的用法仅引用点击元素。
的代码也可以通过简单地链接调用一起被清理一点点:
$('div.trigger').click(function() {
if ($(this).hasClass('open')) {
$(this).removeClass('open').addClass('close').next().slideDown(100);
} else {
$(this).removeClass('close').addClass('open').next().slideUp(100);
}
return false;
});
6
当您使用类别触发器单击div时,它将使用next()
打开触发器旁边的元素。类名不重要,因为它在$(this)
上运行,这是点击元素。
打开下面的div后,它会为自己分配一个“打开”类,以便下次用户点击它时可以采取不同的行为。
该页面的代码是这样的:
$('div.trigger').click(function() {
if ($(this).hasClass('open')) {
$(this).removeClass('open');
$(this).addClass('close');
$(this).next().slideDown(100);
return false;
} else {
$(this).removeClass('close');
$(this).addClass('open');
$(this).next().slideUp(100);
return false;
}
});
其中,翻译成英文,是这样的:
/*
When .trigger is clicked:
If it has a class named 'open':
Remove that class,
and add a class named 'close'.
Slide down the element next to this element in 100 milliseconds.
Prevent other actions from taking place.
If it hasn't got a class named 'open':
Remove class 'close',
and add class 'open'.
Slide up the element next to this element in 100 milliseconds.
Prevent other actions from taking place.
0
$('div.trigger').click(function()...this...
意味着,当你点击一个trigger
类元素时,函数仅适用于this
,这是您单击的元素。当你点击它时,它会得到一个新课程,open
,这给了它新的属性。
0
这是因为你用$运行(本),转换按钮单击。
0
在源代码中的函数$(document).ready()
附加一个新click
功能每$('div.trigger')
元件:
$('div.trigger').click(function() {
// ...
});
在个体(电流)DIV通过$(this)
表示的功能。
if ($(this).hasClass('open')) {
// ...
}
概括地说,每个div有分配了相同的功能和功能码被写入在当前的角度(点击)DIV。
2
一些行内注释可能解释清楚:
#If we have divs with the class 'trigger'
if($('div.trigger').length > 0) {
# Attach logic to the click event of each
$('div.trigger').click(function() {
# If this has the class 'open' already
if ($(this).hasClass('open')) {
# Remove the class 'open'
$(this).removeClass('open');
# Add the class 'close'
$(this).addClass('close');
# And slide down the next div
$(this).next().slideDown(100);
# Return False
return false;
} else {
# If this didn't have 'open', remove 'close'
$(this).removeClass('close');
# Add the class 'open'
$(this).addClass('open');
# And slide up the next div
$(this).next().slideUp(100);
# Return false
return false;
}
});
}
相关问题
- 1. 这为什么有效?
- 2. 为什么这个Prolog谓词有效?
- 3. 为什么不是这个XHTML有效?
- 4. 为什么这个有效的C
- 5. 为什么这个有效的代码?
- 6. 这为什么有效? (sameAs的VS ==)
- 7. 为什么这个TableView代码有效?
- 8. Java:为什么这个子类有效?
- 9. 为什么这个语法有效?
- 10. 这为什么有效? (HTML + PHP)
- 11. 这为什么有效? JNDI,Tomcat6
- 12. 这个程序为什么有效?
- 13. 为什么这个有效的Python?
- 14. 这个源代码为什么有效?
- 15. 为什么这个JavaScript有效?
- 16. jquery slide show - 为什么这段代码有效?
- 17. 你能解释为什么这个jQuery代码有效吗?
- 18. 这个jquery为什么这么慢?
- 19. 悬停效应,jquery,这个jQuery中有什么错误?
- 20. 这个效果的jquery插件?这个效果叫什么?
- 21. 为什么这个XUL代码有效并且Javascript等效不?
- 22. 为什么这个有效的JSON字符串不会生效?
- 23. 为什么String(null)有效?
- 24. 为什么onBeforeFirstShow有效?
- 25. 它为什么有效?
- 26. 为什么target =“blank”有效?
- 27. 为什么csrftoken cookie有效?
- 28. 为什么这jQuery的简单传输效果不起作用?
- 29. 为什么不在jQuery中使用这种弹跳效果?
- 30. 这个SQL为什么有效? (我不认为它应该!)
+1的伪代码 – 2010-01-01 20:24:20