可能重复:
How do I select text nodes with jQuery?访问文本节点旁边的复选框,JQuery的
我试图移动是在文本节点上相邻的选择复选框中的数据复选框。我正在使用jQuery。下面列出了我尝试过的方法和我收到的结果。
<input class="item" type="checkbox" /> Category 1
<input class="item" type="checkbox" /> Category 2
<script>
$('.item').click(function(){
if($(this).attr('checked'))
{
$('#list').append("<span>" + $(this).next() + "<span><br/>")
//$(this).next() just writes "[object Object]"
//$(this).next().text() doesn't write anything
//Also tried $(this).nextSibling.nodeValue but throws "Cannot access property of undefined"
}
else
{
//This is remove the item from the list on uncheck
}
});
</script>
可能会更容易。但'[object Object]'意味着你得到的是DOM元素,而不是它的内容。你试过'$(this).next()。val()'? – Marc 2012-04-22 18:37:37
这个问题与之前关于这个主题的问题完全一样。其答案可能会与另一个相同的问题合并。 http://stackoverflow.com/questions/298750/how-do-i-select-text-nodes-with-jquery – iambriansreed 2012-04-22 18:37:38
$(this).next()。val()在我的列表中写入“undefined”。我真的不想使用其他示例,因为我实际上只是试图访问下一个节点。我会尝试把它放在小提琴中,但复选框项目列表正在由另一个js文件生成。我试图贬低它以专注于这个实际问题。 – ExceptionLimeCat 2012-04-22 19:04:30