代碼如下html:
<div>
<p>內容<span>我不獲取的內容</span>內容</p>
</div>
js:
var content = $("div p").text();
var content = $("div p").html();
實現效果:以上兩種方法text(),html()都要把span標簽內的文本獲取出來,而現在我想獲取p標簽下的“內容”,不包括p標簽下的span標簽下的內容,即 我需要讓content = "內容內容"、而不是content = "內容<span>我不獲取的內容</span>內容內容",也不是content = "內容我不獲取的內容內容內容"
實現方法:
一:
var obj = $("div").children("p").clone(); obj.find(':nth-child(n)').remove(); console.log(obj.html());
二:
var str = $('div p').contents().filter(function (index, content) {
return content.nodeType === 3;
}).text();
