因为英语水平差,想做个浏览器插件:在每个英文下,插入一个中文翻译而不改变原网站结构,然后jquery $().text()方法会把元素下所有text都给提取出来,比如下面的<li><li id="test" value="0">
Guest - Guest
<ul class="task-list">
<li>Role = $everyone, $unauthenticated</li>
<li>
Has access to the "List projects" function, but none of the others
</li>
</ul>
</li>如果用$("#test").text(); 取出来的值是所有TEXT,如下"Guest - GuestRole = $everyone, $unauthenticatedHas access to the "List projects" function, but none of the others",我该如何做才能只取当前的这个值呢?
1 回答

慕容3067478
TA贡献1773条经验 获得超3个赞
var test=$("#test").contents().filter(function() { return this.nodeType === 3; })[0].nodeValue;
这样可以达到题主的要求。
但是建议题主在Guest - Guest
用一个标签包起来,这样获取起来比较直接,写法应该也比较正确。
添加回答
举报
0/150
提交
取消