javascript或jquery select元素中的错误 [英] Error in javascript OR jquery select element

查看:130
本文介绍了javascript或jquery select元素中的错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Jquery:

$("#div td").live("click", function(){

alert("Hellow");

});

Javascript:

Javascript:

function alertH(){

alert("Hellow");

}

HTML Jquery:

HTML Jquery:

<div>

<td>

<a>X</a>

<span> HI! </span>

</td>

</div>

点击td并运行jQuery函数,但只有当你点击td而不是其他标签里面,如jquery?

Click on td and runs the jQuery function, but only if you click on td and not other tags inside, as in jquery?

HTML javascript:

HTML javascript:

<div>

<td onClick="alertH()">

<span> Hummm </span>

<a> Delete </a>

</td>
</div>

如何在javascript中运行,只有在直接点击td而不是在其他html标签中运行内部

How do in javascript, and it only runs if you click directly on td and not in other html tags inside

只有当您单击div功能而不在div中的其他标签中时,才执行此功能。上述两种方法如何?

Performs this function only if you click the div function and not in other tags inside the div. How do the two methods above?

推荐答案

一种方法是通过在不想要调用的每个元素上添加代码一个函数

One way to do it is by adding the code on each element you dont want to call a function

onClick="event.cancelBubble = true;"

另一种方法是检入调用的回调,以查看目标。

Another way is to check in the callback called to see the Target there.

<div onClick="test()" id="run">
Click Works<br>
<label>Not Work</label>
</div>

function test(){
    if (event.target.id !== "run") return false;
    alert(event.target);
}

这篇关于javascript或jquery select元素中的错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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