javascript - Parent() div 的 UL

标签 javascript jquery parent

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;
}​

http://jsfiddle.net/zVx8s/1/

我想做的就是获取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/

相关文章:

javascript - 未捕获的类型错误 : Cannot read property 'setAttribute' of null (console error)

jquery - 垃圾邮件机器人可以触发 mouseenter 或 hover 事件吗?

php - 如何避免ajax响应文本中的html dom代码

javascript - iframe-在不指定域名的情况下更改父级的网址

在 C 中从终端控制进程

c++ - 将对象转换到父级

javascript - 日期字段 : how to set maximum date while using Bootstrap datepicker?

javascript - 向数组元素添加事件

javascript - Mongoose 和 Express 中的两个数据库查询

javascript - jQuery 右侧 slider 面板