如何获取使用jQuery点击的元素的id [英] How to get the id of the element clicked using jQuery

查看:146
本文介绍了如何获取使用jQuery点击的元素的id的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 < div class =main_div> 
< div id =inner_div>
< span id =d1class =get_clicked>点击获取ID< / span>
< / div>
< / div>

如何获取被点击元素的ID?在inner_div内部存在的跨度将具有不同的id,因为我将使用jquery ajax从模型(MVC)加载跨度。所以会有'n'个跨度。所有span将具有唯一的ID。我想获得我点击的跨度的ID。



点击时如何获取跨度的ID?如何使用jQuery?

解决方案

更新为动态加载内容,以便您使用。

  $(document).on('click','span',function(){
alert(this.id);
});

旧代码

  $('span')。click(function(){
alert(this.id);
});

,或者您可以使用 .on

  $('span')。on('click',function b $ b alert(this.id); 
});

/ p>

this.id 将给出当前的 id span clicked


<div class="main_div">
    <div id="inner_div">    
        <span id="d1" class="get_clicked">click to get id</span>
    </div>
</div>

How to get the id of the clicked element? The span which is present inside the inner_div will be having different ids because I will be loading the span from the model(MVC) using jquery ajax. So there will be 'n' number of span. All the span will have unique id. I want to get the id of the span which I click.

How the get the id of the span when clicked? How to do this using jQuery?

解决方案

update as you loading contents dynamically so you use.

$(document).on('click', 'span', function () {
    alert(this.id);
});

old code

$('span').click(function(){
    alert(this.id);
});

or you can use .on

$('span').on('click', function () {
    alert(this.id);
});

this refers to current span element clicked

this.id will give the id of the current span clicked

这篇关于如何获取使用jQuery点击的元素的id的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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