单击任何按钮,但单击一个以显示警报jquery

时间:2018-04-12 01:41:20

标签: jquery

我有一组按钮,我希望在点击任何按钮时显示提醒但#button3

HTML

 <div id="Icon"></div>
 <div id="button1">Click 1</div>
 <div id="button2">Click 2</div>
 <div id="button3">Click 3</div>
  ...
 <div id="buttonn">Click n</div>

JQUERY

  IconDeactived = function(){

  if(!$("#button3") is clicked){ alert();

  $("#Icon").css({
     "background-image" : "url('icons/poll_disabled.png')",
     "pointer-events" : "none"
  });
    }
 }

2 个答案:

答案 0 :(得分:1)

您可以使用 :not pseudo-class 来定位非<div>的所有#button3元素:

&#13;
&#13;
$('div:not(#button3)').on("click", function() {
  console.log('Clicked element: ' + this.id);
});
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<div id="Icon"></div>
<div id="button1">Click 1</div>
<div id="button2">Click 2</div>
<div id="button3">Click 3 -- Won't trigger</div>
...
<div id="buttonn">Click n</div>
&#13;
&#13;
&#13;

显然,您可能会有更多<div>元素,因此您可能需要更改您的选择器,但一般原则保持不变;只需定位您希望点击处理程序应用的所有元素,然后使用:not定位您要排除的子集。

答案 1 :(得分:1)

您可以使用选择器仅侦听非button3元素。

&#13;
&#13;
//only those that start with "button" and not button3
$('div[id$="Icon"]:not(#ExcludeIcon)').on('click', function() {
  alert('clicked');
});
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<div id="Icon"></div>
<div id="PollIcon">Click 1</div>
<div id="GifIcon">Click 2</div>
<div id="ExcludeIcon">Click 3</div>
<div id="ToolIcon">Click n</div>
&#13;
&#13;
&#13;