如何在特定的 DIV 块(JS 或 JQUEry)中获取 ElementByID
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15757105/
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
How to getElementByID in a specific DIV block (JS or JQUery)
提问by BrownChiLD
I just wanted a fast/easy/simple way to check for existing ID on a specific element (div in this case)..
我只是想要一种快速/简单/简单的方法来检查特定元素(在这种情况下为 div)上的现有 ID。
Can't seem to find code sample for this..im using jquery but i dont think i need to do jquery on this one, just basic getElement.. but i need to isolate the search inside a div block.. because the id does exist in other elements on the page but i need to know if it exist in a specific area/div.
似乎无法找到此代码示例..im 使用 jquery 但我不认为我需要在这个上做 jquery,只是基本的 getElement.. 但我需要隔离 div 块内的搜索.. 因为 id 确实存在于页面上的其他元素中,但我需要知道它是否存在于特定区域/div 中。
so instead of just
所以而不是仅仅
document.getElementById(target_id);
i need something like:
我需要类似的东西:
divName.getElementById(target_id);
or
或者
$("document.divName").getElementById(target_id);
or
或者
$(".divName").document.getElementById(target_id);
Can't seem to find something that works.
似乎无法找到有效的东西。
回答by Adil
IDs are supposed to be uniqueand no two elements in page should have same id. You may search some element with some class in div with specific ID.
ID 应该是唯一的,页面中没有两个元素应该具有相同的 ID。您可以在具有特定 ID 的 div 中搜索具有某个类的某些元素。
$('#divId .someClass')
or using find()
或使用find()
$('#divId').find('.someClass')
or using context, jQuery( selector [, context ] )
或使用上下文,jQuery( selector [, context ] )
$('.someClass', $('#divId'))
回答by reuns
var mySubDiv = myParentDiv.querySelector("#mySubDivId")
is equivalent to
相当于
var mySubDiv = document.querySelector("#myParentDivId #mySubDivId");
// don't forget the space : #myParentDiv#mySubDivId won't work
where querySelector
and querySelectorAll
are very useful functions, enough for me to avoid using jQuery
: they accept any css selector
wherequerySelector
和querySelectorAll
是非常有用的函数,足以让我避免使用jQuery
:它们接受任何css selector
in real life, using the same Id for different DOM elements
often happens.
在现实生活中,DOM elements
经常会发生不同的使用相同的Id 。
回答by Sudhir Bastakoti
id
's should be unique, you can check for element using:
id
应该是唯一的,您可以使用以下方法检查元素:
$(".your_parent_div").find("div#some_unique_id");
回答by easa
you can use it for the getElementsByTagName or ClassName, but ID is unique over document. so doesn't need to do that. better to use a special ID. and in every id define as a element in javascript and you can just write id's name and use it, like this : ID.style.color = red;
您可以将它用于 getElementsByTagName 或 ClassName,但 ID 在文档中是唯一的。所以不需要这样做。最好使用一个特殊的ID。并且在每个 id 中定义为 javascript 中的一个元素,您可以编写 id 的名称并使用它,如下所示:ID.style.color = red;
回答by naresh Purushotham
According to my understanding on your question, You have used two id's with same name when u execute, It takes only first ID so you are asking to take id from the specific div, well that is bad type of coding to use two id for same name instead go for class if want to use same name. solution for your question is -this ->
根据我对你的问题的理解,你在执行时使用了两个同名的 id,它只需要第一个 ID,所以你要求从特定的 div 中获取 id,这是错误的编码类型,使用两个 id 相同如果要使用相同的名称,请改用名称去上课。您的问题的解决方案是 -this ->
var someDiv = document.getElementsByClassName("divName");
var someId = someDiv[0].getElementById("target_id");