【发布时间】:2013-11-27 14:23:06
【问题描述】:
我正在使用 liferay 框架,我需要将 JavaScript 检测到的内联高度添加到我页面中非常非常具体的 div 中。问题是我需要通过未知数量的动态添加的 div 和动态添加的类和 ID 来定位它。更复杂的是,这些 div 是随机的兄弟姐妹或相互嵌套。
它是这样的:
<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>
显然我不能简单地用它来定位它
function resize() {
var heights = window.innerHeight;
jQuery('.DIV-I-WANT-TO-TARGET').css('height', heights + "px");
}
resize();
因为该类存在于其他地方,所以我宁愿使用类似的东西来定位它。
jQuery('.known-class .DIV-I-WANT-TO-TARGET')
这显然不起作用,因为中间有很多其他 div,而我的 div 不是“.known-class”的孩子
我问自己是否有任何 jQuery 可以提供帮助。比如:
使用.DIV-I-WANT-TO-TARGET 类“一般”在另一个具有.known-class 的div 中捕获任何div
这可能吗?非常感谢您的帮助!
【问题讨论】:
-
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"- 错误,因为那里没有子选择器,所以它会起作用,所以这只会影响嵌套在 @987654327 内的 ('.DIV-I-WANT-TO-TARGET') @ -
jQuery('.known-class .DIV-I-WANT-TO-TARGET')- 这会工作(虽然如果你在你的类名中使用破折号就不行!)。这会找到任何后代,而不仅仅是孩子。 -
任何具有 .DIV-I-WANT-TO-TARGET 类的 div “一般”在另一个具有 .known-class 的 div 中 是您发布的第二件事
-
您已经在自己的问题中得到了答案..
-
帮我们清理所以你可以删除帖子:)
标签: javascript jquery html css