javascript 获取点击的 div 的 id
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8111682/
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
Get the id of the clicked-upon div
提问by CoreDo
I want to select the id
of the current div when I click on it in jQuery.
For example, say I have HTML like this:
id
当我在 jQuery 中单击它时,我想选择当前 div 的 。
例如,假设我有这样的 HTML:
<div class="item" id="10">hello world</div>
<div class="item_10">hello people</div>
When I click on the first div on .item
class, I want to copy the id
of the current div + adding to it the number (10), so it will be ("div id" + 10)
equal to the second dev class = item_10.
当我单击.item
class上的第一个 div 时,我想复制id
当前 div 的 div + 添加数字 (10),因此它将("div id" + 10)
等于第二个 dev class = item_10。
I tried to use currentid = this.id;
but it doesnt work :( !
我尝试使用currentid = this.id;
但它不起作用:(!
回答by Phrogz
First, note that id
attributes starting with numbers are syntactically illegal in HTML4. If you're using id="10"
make sure that you're using the HTML5 doctype (<!DOCTYPE html>
).
首先,请注意id
以数字开头的属性在 HTML4 中在语法上是非法的。如果您正在使用id="10"
,请确保您使用的是 HTML5 文档类型 ( <!DOCTYPE html>
)。
It's hard to say why what you were doing didn't work without seeing your actual code. Presumably it is because you were registering for the event on a higher element (like the body) and this.id
was the id
of that higher element and not the element you clicked on.
如果没有看到您的实际代码,很难说为什么您正在做的事情不起作用。大概是因为您在更高的元素(如正文)上注册了该事件,并且this.id
是该id
更高元素的 ,而不是您单击的元素。
In this case, you want to use the target
property of the event to find what you clicked on. For example:
在这种情况下,您希望使用target
事件的属性来查找您单击的内容。例如:
$(document.body).click(function(evt){
var clicked = evt.target;
var currentID = clicked.id || "No ID!";
$(clicked).html(currentID);
})
Seen in action: http://jsfiddle.net/Gra2P/
在行动中看到:http: //jsfiddle.net/Gra2P/
If you were registering on the specific elements instead, then this.id
doeswork:
如果您在特定元素上注册,那么this.id
确实有效:
$('div').click(function(evt){
var currentID = this.id || "No ID!";
$(this).html(currentID);
})
Seen in action: http://jsfiddle.net/Gra2P/1/
在行动中看到:http: //jsfiddle.net/Gra2P/1/
This is sub-ideal, however, because:
然而,这是次理想的,因为:
- It makes many event handler registrations instead of 1, and
- If additional divs are added to the document after this code is run, they will not be processed.
- 它使许多事件处理程序注册而不是 1,并且
- 如果在运行此代码后将额外的 div 添加到文档中,它们将不会被处理。
Under jQuery 1.7, you use the .on
method to create a single event handler on a parent element with selectors for the kinds of elements you want to catch the event on, and have this
set to them. In code:
在 jQuery 1.7 下,您可以使用该.on
方法在父元素上创建单个事件处理程序,并为要在其上捕获事件的元素类型设置选择器this
。在代码中:
$(document.body).on('click','div',function(evt){
var currentID = this.id || "No ID!";
$(this).html(currentID);
})
Seen in action: http://jsfiddle.net/Gra2P/2/
在行动中看到:http: //jsfiddle.net/Gra2P/2/
回答by Jayendra
回答by Niels
I think you're trying to do something like:
我认为您正在尝试执行以下操作:
$(".item").click(function(){
var id = $(this).attr("id");
var el = $(".item_" + id);
});
Now el is your second div.
现在 el 是你的第二个 div。
回答by Madara's Ghost
Something like this?:
像这样的东西?:
$('div').click(function() {
theId = $(this).attr('id');
//Do whatever you want with theId.
});
回答by Sudhir Bastakoti
This can be done as:
这可以通过以下方式完成:
$('.item').click(function() { var divId = $(this).attr("id"); });