如何使用Jquery选择内联文本?
如何选择下面无序列表前面的文本?
How can I remove this text?
我尝试过这样的事情:
$('ul.bar').previousSibling().remove();
但我认为这只适用于另一个元素,我不完全确定如何选择不包含在标签内的文本。
是您正在寻找的解决方案。
$('.foo').contents().filter(function(){ return (this.nodeType == 3); }).remove();
在这里演示
nodeType属性返回指定节点的节点类型(数字)。 如果节点是文本节点,则nodeType属性将返回3。
非jQuery版本:
获取父节点。 获取Parent的所有Children节点。 将节点集合转换为数组。 循环遍历数组。 在每次迭代时检查nodeType。 如果nodeType === 3
那么它是一个文本节点。 然后删除它。
Array.prototype.slice.call(document.getElementById('parent_node_id_here').childNodes, 0).forEach(function (value) { if (value.nodeType === 3) { value.remove(); } });
$(".foo").contents().get(0).remove()
或没有jquery
document.getElementsByClassName("foo")[0].childNodes[0].remove()
尝试
$(".foo").replaceWith($.parseHTML($(".foo").html()).slice(1));
$(".foo").replaceWith($.parseHTML($(".foo").html()).slice(1));
How can I remove this text?