jquery:获取对象名称?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5524352/
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
jquery: get the object name?
提问by laukok
I am trying to retrieve the clicked object name through the code below, but I get [object object] only, how can I get the object name?
我试图通过下面的代码检索点击的对象名称,但我只得到 [对象对象],我怎样才能得到对象名称?
<a href="#" class="clickme">click</a>
$('.clickme').click(function(){
alert($(this));
return false;
});
I would like to get '.clickme'
as my object name.
我想'.clickme'
作为我的对象名称。
Thanks.
谢谢。
EDIT:
编辑:
thanks for the help guys. sorry that for not being clear.
感谢您的帮助。抱歉,不清楚。
I want to get the object name dynamically when I turn this jquery click() into a customised function or a plugin.
当我将此 jquery click() 转换为自定义函数或插件时,我想动态获取对象名称。
so,
所以,
<a href="#" class="clickme">click</a>
$('.clickme').click(function(){
alert($(this));
return false;
});
I would like to get '.clickme'
as my object name.
我想'.clickme'
作为我的对象名称。
and,
和,
<a href="#" id="clickme">click</a>
$('#clickme').click(function(){
alert($(this));
return false;
});
I would like to get '#clickme'
as my object name.
我想'#clickme'
作为我的对象名称。
so you notice sometime I want to get id name but sometime I want to get the class name as the object name.
所以您会注意到有时我想获取 id 名称但有时我想获取类名作为对象名称。
采纳答案by mVChr
jQuery does have an internal property selector
that can be used to find what you're looking for:
jQuery 确实有一个内部属性selector
,可用于查找您要查找的内容:
$('.clickme').selector == '.clickme';
$('.clickme').find('a').selector == '.clickme a';
However you can't access this within the click(function(e){})
since you have to reselect using this
:
但是,您无法在 中访问它,click(function(e){})
因为您必须使用this
以下方法重新选择:
$('.clickme').click(function(e){
alert($(this).selector); // alerts nothing
});
However, when you create the click function you can assign that element to a variable and reference it within:
但是,当您创建 click 函数时,您可以将该元素分配给一个变量并在其中引用它:
var $cm = $('.clickme').click(function(e){
alert($cm.selector); // alerts '.clickme'
});
回答by ThiefMaster
That's the class. Not the object name and certainly nothing unique.
这就是班级。不是对象名称,当然也没有什么独特之处。
You can get it using this.className
or $(this).attr('class')
.
您可以使用this.className
或来获取它$(this).attr('class')
。
If you want something unique, use the id
attribute and replace class
with id
.
如果您想要独特的东西,请使用该id
属性并替换class
为id
.
回答by Le Droid
This doesn't responds to the exact question but to the one I got when I get there - So you too maybe ;-)
这并没有回答确切的问题,而是回答了我到达那里时遇到的问题 - 所以你也可能;-)
Short answer: use "this.name" which is the same than "$(this).attr('name')"
简短回答:使用“ this.name”,它与“$(this).attr('name')”相同
Sample:
样本:
$("*").click(function (event) {
event.stopPropagation();
// Display the name of the clicked object
$(this).after("<span> " + this.name +" clicked! </span>");
// Change the name for what you want (optional - just to show it's doable)
this.name = this.name + "X";
});
回答by tvanfosson
Name or id?
姓名还是身?
$('.clickme').click(function(){
alert( this.id ); // or $(this).attr('name') if you want the name
return false;
});
What it really looks like is that you want the class (though, I'm not sure why since you've used the class as the selector).
它真正看起来是你想要这个类(不过,我不知道为什么,因为你已经使用了这个类作为选择器)。
$('.clickme').click(function(){
alert( $(this).attr('class') );
return false;
});
回答by Quincy
an alternative using the event object
使用事件对象的替代方法
$('.clickme').click(function(e){
alert($(e.target).attr('class'));
return false;
});