我正在使用这段代码:
$('body').click(function() {
$('.form_wrapper').hide();
});
$('.form_wrapper').click(function(event){
event.stopPropagation();
});
这个HTML:
<div class="form_wrapper">
<a class="agree" href="javascript:;">I Agree</a>
<a class="disagree" href="javascript:;">Disagree</a>
</div>
问题是,我有链接在div和当他们不再工作时,点击。
摘自https://sdtuts.com/click-on-not-specified-element/
现场演示http://demos.sdtuts.com/click-on-specified-element
$(document).ready(function () {
var is_specified_clicked;
$(".specified_element").click(function () {
is_specified_clicked = true;
setTimeout(function () {
is_specified_clicked = false;
}, 200);
})
$("*").click(function () {
if (is_specified_clicked == true) {
//WRITE CODE HERE FOR CLICKED ON OTHER ELEMENTS
$(".event_result").text("you were clicked on specified element");
} else {
//WRITE CODE HERE FOR SPECIFIED ELEMENT CLICKED
$(".event_result").text("you were clicked not on specified element");
}
})
})
这个解决方案应该工作得很好,很简单:
jQuery(document).ready(function($) {
jQuery(document).click(function(event) {
if(typeof jQuery(event.target).attr("class") != "undefined") {
var classnottobeclickforclose = ['donotcountmeforclickclass1', 'donotcountmeforclickclass2','donotcountmeforclickclass3'];
var arresult = jQuery.inArray(jQuery(event.target).attr("class"), classnottobeclickforclose);
if (arresult < 0) {
jQuery(".popup").hide();
}
}
});
});
在上面的代码更改donotcountmeforclickclass1, donotcountmeforclickclass2等类,你已经用来显示弹出或在它的点击弹出不应该影响,所以你必须明确添加类,你正在使用打开弹出。
用popup class更改.popup class。