简体   繁体   English

jQuery禁用单击但不链接

[英]jQuery disable click but not link

I just want to disable the ability for a user to click, with the exception of links 我只想禁用用户点击的功能(链接除外)

$('selector').children().not('a').click(function(e) {
  return false;
});

this doesn't work.. thanks 这行不通..谢谢

children only selects the immediate children. children只会选择直子。

To disable all of selector 's descendants, use this: 要禁用selector的所有后代,请使用以下命令:

$('selector :not(a)').click(function(e) {
    e.preventDefault()
});

Or, to be a little more efficient, use delegation: 或者,为了更有效一点,请使用委托:

$('selector').on('click', ':not(a)', function(e) {
    e.preventDefault()
});

When you want to disable the user from clicking stuff other than links, simply do this: 如果要禁止用户单击链接以外的内容,只需执行以下操作:

$('selector').find(':not(a)').click(function(e){
    e.preventDefault()
})

It applies to all elements other than links. 它适用于链接以外的所有元素。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM