HTML:
<ul id="column">
<li id="widget8">
<div class="testhub"></div>
</li>
</ul>
JS:
if ($("#widget8").parent("#dock")) {
$(".testhub").html("<p>dock</p>");
}
else {
$(".testhub").html("<p>not dock</p>");
}
CSS:
#dock {
width: 90%;
height: 100px;
background-color: blue;
}
#column {
width: 90%;
height: 100px;
background-color: red;
}
#widget8 {
width: 90%;
height: 50px;
background-color: yellow;
}
.testhub {
width 50%;
height: 25px;
background-color: black;
color: white;
}
我想做的就是获取li所在的ul,如果它在#dock上,则写入一些内容测试中心。但似乎不起作用,请检查 jsfiddle。
我认为我错误地使用了parent。
最佳答案
试试这个:工作演示 http://jsfiddle.net/268mh/
请注意,DOM 中永远不能有相同的 id。所以我已经将 widget8
改为类!
安息吧,希望演示能帮助我们的事业! :)
代码
$(".widget8").each(function() {
if ($(this).parent().prop("id") == "dock") {
$(this).find(".testhub").html("<p>dock</p>");
}
else {
$(this).find(".testhub").html("<p>not dock</p>");
}
});
HTML
<ul id="dock">
<li class="widget8">
<div class="testhub"></div>
</li>
</ul>
<ul id="column">
<li class="widget8">
<div class="testhub"></div>
</li>
</ul>
关于javascript - Parent() div 的 UL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11680589/