<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时无效果。
请问哪里搞错了呢?
天蓬老师2017-04-10 14:29:50
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');
});
});