Jquery 防止默认不工作于<;a>;内部a<;部门>;
我有这个功能:Jquery 防止默认不工作于<;a>;内部a<;部门>;,jquery,preventdefault,Jquery,Preventdefault,我有这个功能: $(document).ready(function() { $('.UIDropDown').click(function () { $('.button', this).click(function(e){ e.preventDefault()}); $(this).addClass('active'); }); }); 以及以下html: <div class="UIDropDown"> &
$(document).ready(function() {
$('.UIDropDown').click(function () {
$('.button', this).click(function(e){ e.preventDefault()});
$(this).addClass('active');
});
});
以及以下html:
<div class="UIDropDown">
<a href="" class="button">button link</a>
<ul class="submenu">
<li><a href="">Item</a></li>
<li><a href="">Item</a></li>
<li><a href="">Item</a></li>
</ul>
</div>
为什么e.preventDefault()在.button上不起作用?页面仍会刷新
Thx,因为您在事件实际发生后绑定了事件处理程序,而它不会被执行。将事件处理程序绑定到DOM就绪事件处理程序中的元素:
$(document).ready(function() {
// Bind event handler to the button
$('.UIDropDown .button').click(function(e) {
e.preventDefault();
});
// Bind event handler to the div (click on .button will trigger both)
$('.UIDropDown').click(function () {
$(this).addClass('active');
});
});
这里有一个.,因为您在事件实际发生后绑定事件处理程序,而它不会被执行。将事件处理程序绑定到DOM就绪事件处理程序中的元素:
$(document).ready(function() {
// Bind event handler to the button
$('.UIDropDown .button').click(function(e) {
e.preventDefault();
});
// Bind event handler to the div (click on .button will trigger both)
$('.UIDropDown').click(function () {
$(this).addClass('active');
});
});
这里有一个。尝试“return false;”而不是preventDefault();这将阻止访问URI。例如:
$('.button', this).click(function(e){ return false; });
尝试“return false;”而不是preventDefault();这将阻止访问URI。例如:
$('.button', this).click(function(e){ return false; });
在div click函数中不需要link click函数 试试这个
$('.UIDropDown').click(function() {
$(this).addClass('active');
});
$('.button').click(function(e) {
e.preventDefault()
});
在div click函数中不需要link click函数 试试这个
$('.UIDropDown').click(function() {
$(this).addClass('active');
});
$('.button').click(function(e) {
e.preventDefault()
});
试试这个
$('.UIDropDown').click(function () {
$('.button', this).click(function(e){ return false; });
$(this).addClass('active');
});
试试这个
$('.UIDropDown').click(function () {
$('.button', this).click(function(e){ return false; });
$(this).addClass('active');
});
你应该写
$('.button').click(function(e){ e.preventDefault()});
在之外,单击$('.ui下拉列表')
您应该编写的处理程序
$('.button').click(function(e){ e.preventDefault()});
在$('.UIDropDown')
的click
处理程序之外,我认为最好是“如果您想禁用click event,我认为最好是”如果您想禁用click eventprevent default
如果事件绑定发生在正确的位置,它将正常工作。请参阅我的答案。preventDefault
如果事件绑定发生在正确的位置,则可以正常工作。看我的答案。那没什么区别<如果事件绑定发生在正确的位置,code>preventDefault
将正常工作。看我的答案。那没什么区别<如果事件绑定发生在正确的位置,code>preventDefault
将正常工作。查看我的答案。当您将.button
事件绑定移动到其他事件处理程序之外时,您可能不需要此
上下文参数。当您将按钮
事件绑定移动到其他事件处理程序之外时,您可能不需要此
上下文参数。