You can either recursively walk down the DOM looking at the textContent or innerText property (as appropriate) of elements, or you can use loop over the collection returned by getElementsByTagName. Either way, once you have identified the text and the parent element, you need to work out how to replace it.
What are your requirements for document structure in the replacement if the string is split over one or more othere elements?
You could use jQuery selectors to get the <b> tags containing John that come after a <span> tag that contains First name:, and then for instance apply a style:
发布评论
评论(3)
您可以递归地遍历 DOM,查看元素的 textContent 或 innerText 属性(视情况而定),也可以对 getElementsByTagName 返回的集合使用循环。无论哪种方式,一旦确定了文本和父元素,您就需要弄清楚如何替换它。
如果字符串拆分为一个或多个其他元素,您对替换中的文档结构有何要求?
You can either recursively walk down the DOM looking at the textContent or innerText property (as appropriate) of elements, or you can use loop over the collection returned by getElementsByTagName. Either way, once you have identified the text and the parent element, you need to work out how to replace it.
What are your requirements for document structure in the replacement if the string is split over one or more othere elements?
您可以使用 jQuery 选择器 来获取
标签包含
John
,位于包含First name:
的标记之后,然后例如应用样式:
这是一个正在运行的示例:http://jsfiddle.net/XzwNj/
You could use jQuery selectors to get the
<b>
tags containingJohn
that come after a<span>
tag that containsFirst name:
, and then for instance apply a style:Here is a running example: http://jsfiddle.net/XzwNj/
你尝试过这样的事情吗?
document.body.innerHTML.replace(/<\/?[^>]+>/g, '')
Did you try something like this?
document.body.innerHTML.replace(/<\/?[^>]+>/g, '')