如何确定是否在Jquery中单击了孩子 [英] How to determine if child was clicked in Jquery

查看:27
本文介绍了如何确定是否在Jquery中单击了孩子的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果 onclick 事件绑定到父级,jQuery 中是否有办法判断单击了哪个子级?

例如:

 $('#daddy').click(function () {//if($("#son").isClicked()){return true;}//返回假;});

标记看起来像:

<div id="daddy"><span id="儿子"></span><span id="女儿"></span></div>

解决方案

您传递给 click 的事件处理程序将接收一个 event 对象作为其第一个参数.事件的target(即发起事件的元素)将在此处指定.

这是 jQuery 文档中的一个示例:

<!DOCTYPE html><html><头><风格>跨度,强,p {填充:8px;显示:块;边框:1px 实心 #999;}</风格><script src="http://code.jquery.com/jquery-latest.js"></script></头><身体><div id="log"></div>

<p><strong><span>点击</span></strong></p></div><脚本>$("body").click(函数(事件) {$("#log").html("点击:" + event.target.nodeName);});</脚本></身体></html>

Is there a ways in jQuery to tell which child was clicked if the onclick event was binded to the parent?

For instance:

  $('#daddy').click(function () {
    // if($("#son").isClicked()){return true;}
    //return false;
  });

and the markup looks like:

<div id="daddy">
  <span id="son"></span>
  <span id="daughter"></span>
</div>

解决方案

The event handler you pass to click will receive an event object as its first argument. The target of the event (i.e. the element that initiated the event) will be specified there.

Here's an example from the jQuery documentation:

<!DOCTYPE html>
<html>
<head>
  <style>
span, strong, p { 
  padding: 8px; display: block; border: 1px solid #999;  }
    </style>
  <script src="http://code.jquery.com/jquery-latest.js"></script>
</head>
<body>

<div id="log"></div>
<div>
  <p>
    <strong><span>click</span></strong>
  </p>
</div>
<script>

$("body").click(function(event) {
  $("#log").html("clicked: " + event.target.nodeName);
});

</script>  
</body>
</html>

这篇关于如何确定是否在Jquery中单击了孩子的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆