javascript - JQurey选择器,如何选中父下的兄弟元素
怪我咯
怪我咯 2017-04-10 14:27:50
[JavaScript讨论组]


<p class="video_items"> <p class="record_name"></p> <p class="postdate">发表于 3天前</p> <p class="edit" style="display: block;"><input type="checkbox" id="edit_item" name="options[]" value="90"> </p> </p> <p class="video_items"> <p class="record_name"></p> <p class="postdate">发表于 3天前</p> <p class="edit" style="display: block;"><input type="checkbox" id="edit_item" name="options[]" value="90"> </p> </p> <p class="video_items"> <p class="record_name"></p> <p class="postdate">发表于 3天前</p> <p class="edit" style="display: block;"><input type="checkbox" id="edit_item" name="options[]" value="90"> </p> </p>
<script type="text/javascript">
$(document).ready(function(){
$("#edit_item").click(
function(){
$(this).parent().parent().find('.record_name').css('text-decoration','line-through');
});
});
</script>

现在js代码只对第一个#edit_item生效,点其他#edit_item时无效果。
请问哪里搞错了呢?

怪我咯
怪我咯

走同样的路,发现不同的人生

全部回复(3)
天蓬老师

jQuery 文档有详细说明:

Each id value must be used only once within a document. If more than one element has been assigned the same ID, queries that use that ID will only select the first matched element in the DOM. 

如果希望对多个 DOM 元素起作用,例如你举的例子,可以这样写:

$(document).ready(function(){
  $(".edit").click(
  function(){
    $(this).parent().parent().find('.record_name').css('text-decoration','line-through');
  });
});
PHPz

那个……在同一个页面里,相同的 ID 只能存在一个……您用错了,这不关 jQuery 的事儿。

黄舟

.closest('.video_items').find('.record_name')这样就直接找到

热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号