将锚点替换为它的innerHTML
我想用其innerHTML 替换链接,如何完成?
示例:
Bla bla bla <a href="#nogo">No link here</a> bla bla bla
必须成为
Bla bla bla No link here bla bla bla
尝试用其innerHTML替换节点,但我无法获取节点本身中的文本..
我想我必须使用正则表达式,但我不知道如何操作。
编辑:感谢大家的快速回复!我似乎还是没能搞清楚。看来还有更多事情发生。
我也在使用原型js。
我收到一些可能包含链接的文本。如果文本包含链接,则应将其替换为它的innerHTML。 它看起来像这样:
<td id="text">This is some text <a href="/link/Go_%28To%29" title="Go (to)">goto</a>. Some more text.</td>
我尝试使用 hasChildNodes() 来检查文本是否包含链接,但它总是返回 yes,即使文本不包含链接或其他元素。所以我想也许文本也被认为是一个节点并尝试了 childElements[1]。返回未定义。所以也许链接是文本节点的一个节点。没有什么! 我正在抓取这些数据并评估 JSON,所以奇怪的地方可能就来自于此。不过,在响应的其余部分中遍历 DOM 进展顺利。
但是,当我发出警报时,
$('text').childElements()[0]
实际的 href 就会收到警报! (localhost//link/Go_%28To%29)
$('text'.childElements()[0].up() 让我到达实际的 td 父元素。
所以我使用 来
if($('text').childElements()[0])
检查文本是否包含链接。
我想这有点偏离主题,但我实际上无法正确理解 a 元素。有什么提示吗? 也许正则表达式是最好的选择?
I want to replace a link with its innerHTML, how is it done?
Example:
Bla bla bla <a href="#nogo">No link here</a> bla bla bla
has to become
Bla bla bla No link here bla bla bla
Tried to replace the node with its innerHTML but I can't get the text in the node itself..
I guess I have to work with regular expressions but I can't figure out how.
EDIT: Thank you all for the quick responses! I still can't seem to get it right. It looks like there's more going on.
I am using prototype js as well.
I got some text with could contain a link. If the text contains a link it should be replaced by it's innerHTML.
It looks something like this:
<td id="text">This is some text <a href="/link/Go_%28To%29" title="Go (to)">goto</a>. Some more text.</td>
I tried using hasChildNodes() to check wether the text contained a link but it always returns yes, even when the text did not contain a link or other element. So I thought that maybe the text is considered a node too and tried childElements[1]. That returns undefined. So maybe the link is a node of the textnode then. Nothing!
I am scraping this data and evalling the JSON so the weirdness may come from that. Walking the DOM in the rest of the response goes well though..
However when I alert
$('text').childElements()[0]
The actual href get's alerted! (localhost//link/Go_%28To%29)
$('text'.childElements()[0].up() gets me to the actual td parent element.
So I am using
if($('text').childElements()[0])
To check wether the text contains a link.
I guess this gets a bit off-topic but I actually can't get the a element right. Any tips?
Maybe a regexp is the best option?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
试试这个:
Try this:
将
outerHTML
设置为innerHTML
...Set
outerHTML
to theinnerHTML
...我强烈建议使用其中一个 Javascript 库来完成此类工作。
这里有一个使用 Prototype 的单行代码可以解决这个问题:
它只是迭代每个 。页面中的标签并将其替换为它的innerHTML。
jQuery 对于此类事情也非常有用。
I highly recommend using one of the Javascript libraries for this sort of work.
Here's a one-liner using Prototype that will do the trick:
It simply iterates through each <a> tag in the page and replaces it with its innerHTML.
jQuery is also terrific for this sort of thing.
或者另一种方式,中和链接(没有jquery,我不喜欢它):
创建测试链接:
并取消设置href属性:
注意,链接仍然有它的样式,你也必须重置它
Or another way, neutralizes a link (w/o jquery, i dislike it):
create test link:
and unset href attribute:
note, link still have its style, you have to reset it also
最后我用一个丑陋的
replace(/<\/?[^>]+(>|$)/g, "\n")
解决了这个问题。还有很多其他事情发生,我没有很好地记录下来,这是我的错。In the end I solved the problem with an ugly
replace(/<\/?[^>]+(>|$)/g, "\n")
. There were a lot of other things going on which I haven't documented well, my fault.简而言之,这将完成您的工作:
var a = document.links;
for( var x = 0; x < a.length; x++ ){
a[x].outerHTML = a[x].innerHTML; x-- };
或者,可以使用一些“纯脚本魔法”,或者面向未来的经典,来代替之前的悖论:
This would, in short, do your job:
var a = document.links;
for( var x = 0; x < a.length; x++ ){
a[x].outerHTML = a[x].innerHTML; x-- };
Or instead of the previous paradox, one could use some "pure scripting magic", or a future proof classic: