javascript 获取单击的类元素 jquery 的名称值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11892684/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
getting name value of clicked on class element jquery
提问by Jake
I am currently trying the following but it keeps getting the first name value of the first element with the class "button-blue". How can I work correctly so that it gets the name value of the element clicked on with this class??
我目前正在尝试以下操作,但它不断获取类“button-blue”的第一个元素的名字值。我如何才能正确工作,以便它获取使用此类单击的元素的名称值?
$(".button-blue").click(function() {
alert($(".button-blue").attr('name'));
});
回答by ThiefMaster
Use this.name
instead. Inside an event handler, this
is the DOM element on which the event was triggered.
使用this.name
来代替。在事件处理程序中,this
是触发事件的 DOM 元素。
If you prefer to use .attr()
or need a jQuery object containing that element for another reason, you can simply use $(this).attr('name')
如果您.attr()
出于其他原因更喜欢使用或需要包含该元素的 jQuery 对象,您可以简单地使用$(this).attr('name')
回答by Berend de Boer
Inside an event handler, this.tagName should give you the name.
在事件处理程序中, this.tagName 应该为您提供名称。