简体   繁体   English

jquery 在 div id 中选择 div id

[英]jquery selecting div id within div id

Through jquery I am trying to target a div with an id within a div container with a different id.通过 jquery 我试图在具有不同 id 的 div 容器中定位具有 id 的 div。 Example:例子:

<div id="container-type-a">
<div id="inner_number_2201"></div> //<<<< this id is a passed dynamic ID and changes. Its passed into the function. 
</div>

So I would want to do something like document.getElementById('> container-type-a' > 'inner_number_' + id + ) but this is not the right syntax.所以我想做类似document.getElementById('> container-type-a' > 'inner_number_' + id + )但这不是正确的语法。 Can something like this be done?可以做这样的事情吗?

ID should be unique in the HTML, so unless your HTML is malformed, you should be able to just ID 在 HTML 中应该是唯一的,因此除非您的 HTML 格式错误,否则您应该能够

document.getElementById(id_In_Restaurant)

You can use below code if your dynamic div is direct decedent.如果您的动态 div 是直接死者,您可以使用以下代码。

$('#'+$('#container-type-a >div').attributes.id.value)

If you want to use a selector, you would use querySelector如果要使用选择器,可以使用 querySelector

var inner = document.querySelector("#container-type-a #inner_number_" + id_In_Restaurant);

But since ids are supposed to be unique, it makes no sense to use two ids.但是由于 id 应该是唯一的,所以使用两个 id 是没有意义的。 Just been to use getElementById刚刚使用 getElementById

var inner = document.getElementById("inner_number_" + id_In_Restaurant);

Getting a reference to the parent element获取对父元素的引用

var inner = document.getElementById("inner_number_" + id_In_Restaurant);
var innersParent = inner.parentElement;

Since Id is unique throughout the document so you can use the following syntax由于 Id 在整个文档中是唯一的,因此您可以使用以下语法

$('#inner_number_' + unique_dynamic_id)

This will return you the jquery object of the element.这将返回元素的 jquery object。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM