将 的文本转换为超链接
更新:
答案#3 最终效果最好。我很可能对其他建议做错了; #3 可能是最容易实现的。如果您好奇,可以在这里找到我尝试过的示例解决方案(目前):
- http: //dl.dropbox.com/u/1007716/spanToUrl/test01.html
- http://dl.dropbox.com/u /1007716/spanToUrl/test02.html
- http://dl.dropbox.com/u/1007716/spanToUrl/ test03.html(获胜者)
- http://dl.dropbox.com/u/1007716/spanToUrl/test04.html
- http://dl.dropbox.com/u/1007716/spanToUrl/test05.html
- http: //dl.dropbox.com/u/1007716/spanToUrl/test06.html
原始帖子:
我在 标记内有一个纯文本网站地址。我想改变这个使用
target="_blank"
标记到正确的超链接中
我已经整理了一个详细的示例,说明我必须在这里使用的内容:http://bit.ly/spantourl
如果您不想点击,这是我所拥有的:
<span>http://www.domain.com/about</span>
我需要将其更改为:
<a href="http://www.domain.com/about" target="_blank">http://www.domain.com/about</a>
UPDATE:
Answer #3 ended up working the best. I most likely did something wrong with the other suggestions; #3 was maybe the easiest to implement. If you are curious, the example solutions that I tried can be found here (for now):
- http://dl.dropbox.com/u/1007716/spanToUrl/test01.html
- http://dl.dropbox.com/u/1007716/spanToUrl/test02.html
- http://dl.dropbox.com/u/1007716/spanToUrl/test03.html (winner)
- http://dl.dropbox.com/u/1007716/spanToUrl/test04.html
- http://dl.dropbox.com/u/1007716/spanToUrl/test05.html
- http://dl.dropbox.com/u/1007716/spanToUrl/test06.html
ORIGINAL POST:
I have a plain text website address inside a <span>
tag. I'd like to change that <span> tag into a proper hyperlink with target="_blank"
I've put together a detailed example of what I have to work with here: http://bit.ly/spantourl
If you don't want to click through, here is what I have:
<span>http://www.domain.com/about</span>
and I need to change that into:
<a href="http://www.domain.com/about" target="_blank">http://www.domain.com/about</a>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
试试这个:
不需要
each
,因为 ReplaceWith 可以迭代多个元素,并且可以将函数作为参数。查看您的示例 HTML,我发现它只是第一个包含 URL 的
。如果确实只有一个,您可以将
first()
添加到选择器链中,如下所示:如果是包含链接的整个列,那么您将想要对所有其他比赛进行操作。通过将
:even
附加到选择器来执行此操作,如下所示:(是的,
:even
而不是:odd
来选择第 1 个、第 3 个、 &c. 元素,因为基于 0 的索引。)Try this:
There's no need for
each
, since replaceWith can iterate through multiple elements and can take a function as a parameter.Looking at your sample HTML, I see that it is only the first
<td>
that contains a URL. If there is indeed only one, you can addfirst()
to the selector chain, like this:If it is rather the entire column that contains links, than you'll want to operate on every other match. Do this by appending
:even
to your selector, like this:(Yes,
:even
and not:odd
to select the 1st, 3rd, &c. elements, because of 0-based indexing.)您需要某种形式的标识才能进行从节点 A 到节点 B 的转换。我建议按照以下方式进行操作:
JQuery 代码:
HTML:
上面的代码未经测试,但希望能够引导您进入正确的方向。
You need to have some form of identification in order to do the conversion from node A to node B. I would suggest something along the following lines:
The JQuery code:
The HTML:
The above code is not tested, but should hopefully lead you in the right direction.
使用 jQuery。
给 span 一个 id:
jQuery 代码:
Use jQuery.
give the span an id:
jQuery code:
将跨度设置为 id,然后您可以执行以下操作:
根据您的编辑进行更改
查看工作演示
Put the span an id and then you can do something like
Changing according to your edit
See a working demo
也许是这样的:(不需要知道 ids/classes)使用 jquerys for-each 循环并特定于 tds
内部的目标范围$(文档).ready(函数(){
$('td span').each(function(){
$(this).text("" +
$(this).text() + "");
});
});
编辑:这段代码效果更好:
原版使用了 jquery 的
.text()
函数,其中 html 转义了<>
字符,无意中添加了 <代码>> < 进入 dom,而 .html 实际上输出了正确的标签Perhaps something like this: (no need to know ids/classes) useing jquerys for-each loop and specificly target spans inside of tds
$(document).ready(function(){
$('td span').each(function(){
$(this).text("" +
$(this).text() + "");
});
});
EDIT:this code works much better:
The origional used the
.text()
function of jquery which html escaped the<>
characters, unintentionally addin> <
into the dom, while .html actually outputs the correct tags