获取被点击的项目的ID

获取被点击的项目的ID

本文介绍了获取被点击的项目的ID的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个html代码:

 < b class =editid =foo1> FOO< / b个
< b class =editid =foo2> FOO2< / b>
< b class =editid =foo3> FOO3< / b>

我在jQuery中有这样的代码:



//获取b.edit的ID:例如foo1,foo2,foo3 $ b $($ b $ ed $ p'$'code> $('b.edit')。 b $('。editP')。focus();
});

如何获得 id b.edit ,因为有b.edit的多个实例,我想要获得特定的 id of一个点击?我该怎么做?



感谢,对不起,我对JavaScript很新颖。

解决方案

我从你的示例代码假设你正在使用jQuery?如果是这样,你可以得到如下的ID:

  $('b.edit')。click(function(){
this.id;
});

编辑:

直接引用如果所需要的仅仅是id,属性确实更有效率。



也可以从jQuery对象中获得:


$ b点击(功能(){
$(this).attr('id');
} $ b pre code $ $('b.edit' );

小提琴:


I have this html code:

<b class = "edit" id = "foo1">FOO</b>
<b class = "edit" id = "foo2">FOO2</b>
<b class = "edit" id = "foo3">FOO3</b>

And I have this code in jQuery:

$('b.edit').click(function(){
    //GET THE ID OF THE b.edit: e.g foo1, foo2, foo3
    $('.editP').focus();
});

How can I get the id value of the b.edit, as there are multiple instances of b.edit, and I want to get the specific id of the one clicked? How can I do this?

Thanks, Sorry, I am pretty new to javascript.

解决方案

I'm assuming from your sample code that you're using jQuery? If so you can get the id as follows:

$('b.edit').click(function(){
 this.id;
});

EDIT:

The direct reference to the attribute is indeed more efficient if all that is required is simply the id.

Also can be obtained from the jQuery object:

$('b.edit').click(function(){
 $(this).attr('id');
});

Sample fiddle: http://jsfiddle.net/5bQQT/

这篇关于获取被点击的项目的ID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-24 02:25