简体   繁体   English

在嵌套的动态添加的 div 海洋中定位一个 div

[英]targeting a div in an ocean of nested dynamically added divs

I'm using the liferay framework and I need to add a JavaScript detected inline height to a very very specific div in my page.我正在使用 liferay 框架,我需要将 JavaScript 检测到的行内高度添加到我页面中非常非常具体的 div 中。 The problem is I need to target it going through an unknown number of dynamically added divs with dynamically added classes and IDs.问题是我需要通过未知数量的动态添加的 div 和动态添加的类和 ID 来定位它。 To complicate this even further, the divs are randomly siblings or nested in each other.更复杂的是,这些 div 是随机的兄弟或相互嵌套。

Here's what it looks like:这是它的样子:

<div class="known-class">
<div class="unknown dynamicallygenerated"></div>
<div class="unknown dynamicallygenerated">
    <div class="unknown dynamicallygenerated">
        <div class="unknown dynamicallygenerated"></div>
        <div class="unknown dynamicallygenerated">
            <div class="DIV-I-WANT-TO-TARGET">this is the div i need to Target with my css/javascript</div>
        </div>
    </div>
</div>

obviously I can't target it simply with显然我不能简单地瞄准它

function resize() {

    var heights = window.innerHeight;
    jQuery('.DIV-I-WANT-TO-TARGET').css('height', heights + "px");
}

resize();

Because that class is present elsewhere, I would rather target it with something like.因为该类存在于其他地方,所以我宁愿用类似的东西来定位它。

jQuery('.known-class .DIV-I-WANT-TO-TARGET')

Which obviously doesn't work because there's a ton of other divs in the middle and my div is not a child of ".known-class"这显然不起作用,因为中间有很多其他 div,而我的 div 不是“.known-class”的孩子

I was asking myself if there was any jQuery that could help.我在问自己是否有任何 jQuery 可以提供帮助。 Something like:类似的东西:

Catch any div with .DIV-I-WANT-TO-TARGET class that is "generically" inside another div that has .known-class使用.DIV-I-WANT-TO-TARGET类捕获任何 div, .DIV-I-WANT-TO-TARGET “一般”位于另一个具有.known-class div 中

Is this possible?这可能吗? thanks a lot for your help!非常感谢您的帮助!

Something like this would work:像这样的事情会起作用:

// this will target the known-class and find all children with DIV-I-WANT-TO-TARGET
$('div.known-class').find('div.DIV-I-WANT-TO-TARGET');

// this will target the known-class and find the first DIV-I-WANT-TO-TARGET
$('div.known-class').find('div.DIV-I-WANT-TO-TARGET').first();
$('div.known-class').find('div.DIV-I-WANT-TO-TARGET:first');
$('div.known-class').find('div.DIV-I-WANT-TO-TARGET:eq(0)');
$('div.known-class').find('div.DIV-I-WANT-TO-TARGET').eq(0);

You can try in your css file你可以试试你的css文件

 .known-class div div div div{}

The last div being the DIV-I-WANT-TO-TARGET最后一个 div 是 DIV-I-WANT-TO-TARGET

Assuming that you are adding the divs starting from the outer to the inner假设您正在添加从外部到内部的 div

Assign an equal name plus a number starting from 1分配一个相同的名称加上一个从 1 开始的数字

<div class="known-class">
  <div class="unknown dynamicallygenerated" id="dynamicdiv1"></div>
    <div class="unknown dynamicallygenerated" id="dynamicdiv2">
       <div class="unknown dynamicallygenerated" id="dynamicdiv3">
           <div class="unknown dynamicallygenerated" id="dynamicdiv4"></div>
              <div class="unknown dynamicallygenerated" id="dynamicdiv5">
                <div class="DIV-I-WANT-TO-TARGET" id="dynamicdiv6"></div>
        </div>
    </div>
</div>

The use jQuery [.each][1] to loop through all the divs on the document使用 jQuery [.each][1] 循环遍历文档上的所有 div

$( document.body ).click(function() {
  $( "div" ).each(function( i ) {
    if ( this.style.color !== "blue" ) {
      this.style.color = "blue";
    } else {
      this.style.color = "";
    }
  });
});

When you reach the last item in numeric order.当您按数字顺序到达最后一项时。 (you can use any split function) add the attributes to that div (您可以使用任何拆分功能)将属性添加到该 div

you need to select last div inside the known-class:您需要选择已知类中的最后一个 div:

$('.known-class').find('div:last').css('background', 'Red')

OR if you want to select all the .known-class :或者,如果您想选择所有 .known-class :

$('.known-class').each(function() {$(this).find('div:last').css('background', 'Red')});

Actually your selector works just fine:实际上你的选择器工作得很好:

$('.known-class .DIV-I-WANT-TO-TARGET')

With a space, selectors will find any descendant.使用空格,选择器将找到任何后代。

The search is only limited to direct descendants (immediate children) if you use the > operator.如果您使用>运算符,则搜索仅限于直接后代(直接子代)。

So $('.known-class > .DIV-I-WANT-TO-TARGET') would not find what you wanted.所以$('.known-class > .DIV-I-WANT-TO-TARGET')不会找到你想要的。

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

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