javascript - JQurey选择器,如何选中父下的兄弟元素
怪我咯
怪我咯 2017-04-10 14:27:50
0
3
699


<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)
Peter_Zhu

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');
  });
});
PHPzhong

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

黄舟

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

熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!