[英]jquery slide down div on link click, then slide up when clicking outside of div
Anyone know how I can have a link which when clicked slides down a div, then the div slides back up when anywhere outside the div is clicked ? 有人知道我如何获得一个链接,该链接在单击div时会向下滑动div,而在div之外的任何位置单击时div会向上滑动?
Thnx in advance. 提前thnx。
Scott. 斯科特。
Try this: 尝试这个:
$(".clicker").click(function(e) {
e.preventDefault();
e.stopPropagation();
// do your worst, i.e. slide down
$("div").slideDown("slow");
});
$(document).click(function() {
// slide up
$("div").slideUp("slow");
});
And just to make sure the div is not affected either: 并确保div也不受影响:
$("div").click(function(e) {
e.preventDefault();
e.stopPropagation();
});
Try this: 尝试这个:
$(".target").click(function(e) {
e.preventDefault();
e.stopPropagation();
$(".class").toggle();
});
Found a very good answer by Frederik in Document click not in elements jQuery 在文档单击而非元素jQuery中找到了Frederik的很好的答案
Modified a bit to support elements not yet present in document for example elements fetched by ajax calls. 进行了一些修改,以支持文档中尚不存在的元素,例如由ajax调用获取的元素。
$(document).on('click',function(event) {
if (!$(event.target).closest("#selector").length) {
if ($('#selector').is(":visible"))
$('#selector').slideUp();
}
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.