Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/69.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 在嵌套的div jquery中按类查找id_Javascript_Jquery_Events_Event Handling_Mouseevent - Fatal编程技术网

Javascript 在嵌套的div jquery中按类查找id

Javascript 在嵌套的div jquery中按类查找id,javascript,jquery,events,event-handling,mouseevent,Javascript,Jquery,Events,Event Handling,Mouseevent,我有一个像这样的html <div class='click' id='1'> one <div class='click' id='2'> two <div class='click' id='3'> three <div class='click' id='4'> four <div class='click' id='5'>

我有一个像这样的html

<div class='click' id='1'>
one
<div class='click' id='2'>
    two
    <div class='click' id='3'>
        three
        <div class='click' id='4'>
            four
            <div class='click' id='5'>
                five
            </div>
        </div>
    </div>
</div>

一
二
三
四
五

如果我在类click上有并单击了event,那么有任何方法可以返回我单击的id 比如 $('.click')。单击(函数(){ 警报('我单击的id')

})); 因为如果我点击三,我总是会得到一和二三的id。

当然,只要这样做:

$('.click').click(function(e){ //e=event
    alert($(this).attr("id")); // alert clicked element's id
    e.stopPropagation(); // stop event propagation so it doesnt propagate to click 1 and click 2
})
更新:如Felix Kling所述,您可以直接访问de DOM并使用:

alert(this.id);
当然,只要这样做:

$('.click').click(function(e){ //e=event
    alert($(this).attr("id")); // alert clicked element's id
    e.stopPropagation(); // stop event propagation so it doesnt propagate to click 1 and click 2
})
更新:如Felix Kling所述,您可以直接访问de DOM并使用:

alert(this.id);
演示:使用
this.id

如果你喜欢:

Stop propogation将停止
click
事件,防止事件在DOM树中冒泡,从而防止任何父处理程序收到事件通知

API:

  • .stoppropagation
    -
代码

$(document).ready(function () {

    $('.click').click(function (event) {
        event.stopPropagation();
        alert($(this).prop("id")); //<< --- or this.id

    });

});
$(文档).ready(函数(){
$('.click')。单击(函数(事件){
event.stopPropagation();
警报($(this.prop(“id”);//演示:使用
this.id

如果你喜欢:

Stop propogation将停止
click
事件,防止事件在DOM树中冒泡,从而防止任何父处理程序收到事件通知

API:

  • .stoppropagation
    -
代码

$(document).ready(function () {

    $('.click').click(function (event) {
        event.stopPropagation();
        alert($(this).prop("id")); //<< --- or this.id

    });

});
$(文档).ready(函数(){
$('.click')。单击(函数(事件){
event.stopPropagation();
警报($(this.prop(“id”);//是的。很简单

$(document).ready(function(){
$('.click').click(function(){
var ID=$(this).attr('id');
alert(ID);
//This ID varible will return ID of the Div Clicked
});
});
是的,很简单

$(document).ready(function(){
$('.click').click(function(){
var ID=$(this).attr('id');
alert(ID);
//This ID varible will return ID of the Div Clicked
});
});

通过直接在DOM元素上访问属性,获取ID就更容易了:
this.ID
。它就像一个符咒一样工作。非常感谢你,你真是太棒了♥通过直接在DOM元素上访问属性,获取ID就更容易了:
this.ID
。它就像一个符咒一样工作。非常感谢你,你真是太棒了♥谢谢你,我的日子过得很好♥@霍特鲁姆库我很高兴这对你有帮助(我的一天节省了这么多钱♥@KhoaTrumcuoi很高兴它帮助了
:)
而没有阻止事件
e
冒泡,您希望看到有多少警报被点击?如果没有阻止事件
e
冒泡,您希望看到有多少警报被点击?