jq取值问题

如下,使用jq取div.text()会变成 hello world,有没有什么办法只取div的值hello,不包括span的值呢?
`
<div>hello<span>world</span></div>
`

阅读 1.8k
2 个回答

过滤一下?

$('<div>hello<span>world</span></div>').contents().filter(function (index, con) {
    return con.nodeType === 3;
}).text()

$(div)[0].innerText || $(div).get(0).innerText

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题