的文本转换为超链接

发布于 2024-10-20 08:27:49 字数 1024 浏览 1 评论 0原文

更新:
答案#3 最终效果最好。我很可能对其他建议做错了; #3 可能是最容易实现的。如果您好奇,可以在这里找到我尝试过的示例解决方案(目前):

  1. http: //dl.dropbox.com/u/1007716/spanToUrl/test01.html
  2. http://dl.dropbox.com/u /1007716/spanToUrl/test02.html
  3. http://dl.dropbox.com/u/1007716/spanToUrl/ test03.html(获胜者)
  4. http://dl.dropbox.com/u/1007716/spanToUrl/test04.html
  5. http://dl.dropbox.com/u/1007716/spanToUrl/test05.html
  6. 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):

  1. http://dl.dropbox.com/u/1007716/spanToUrl/test01.html
  2. http://dl.dropbox.com/u/1007716/spanToUrl/test02.html
  3. http://dl.dropbox.com/u/1007716/spanToUrl/test03.html (winner)
  4. http://dl.dropbox.com/u/1007716/spanToUrl/test04.html
  5. http://dl.dropbox.com/u/1007716/spanToUrl/test05.html
  6. 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

小霸王臭丫头 2024-10-27 08:27:49

试试这个:

$('.sampleClass span').replaceWith(function() {
    var url = $.trim($(this).text());
    return '<a href="' + url + '" target="_blank">' + url + '</a>';
});

不需要 each,因为 ReplaceWith 可以迭代多个元素,并且可以将函数作为参数。

查看您的示例 HTML,我发现它只是第一个包含 URL 的 。如果确实只有一个,您可以将 first() 添加到选择器链中,如下所示:

$('.sampleClass span').first().replaceWith( /* ... */ );

如果是包含链接的整个,那么您将想要对所有其他比赛进行操作。通过将 :even 附加到选择器来执行此操作,如下所示:(

$('.sampleClass span:even').first().replaceWith( /* ... */ );

是的,:even 而不是 :odd 来选择第 1 个、第 3 个、 &c. 元素,因为基于 0 的索引。)

Try this:

$('.sampleClass span').replaceWith(function() {
    var url = $.trim($(this).text());
    return '<a href="' + url + '" target="_blank">' + url + '</a>';
});

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 add first() to the selector chain, like this:

$('.sampleClass span').first().replaceWith( /* ... */ );

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:

$('.sampleClass span:even').first().replaceWith( /* ... */ );

(Yes, :even and not :odd to select the 1st, 3rd, &c. elements, because of 0-based indexing.)

允世 2024-10-27 08:27:49

您需要某种形式的标识才能进行从节点 A 到节点 B 的转换。我建议按照以下方式进行操作:

JQuery 代码:

<script type="text/javascript">

    $('.convertableIdentifier').each(function(i, el) {

        // grab the url and the link text
        var url = $(el).html();

        // create a new node with query by decorating a
        // empty a tag
        var newNode = $('<a></a>').attr('href', url).attr('target', '_blank').html(url);

        // replace the current node with our new node
        $(el).replaceWith(newNode);

    });

</script>

HTML:

<span class="convertableIdentifier">http://www.google.com</span>
<span class="convertableIdentifier">http://www.youtube.com</span>
<span class="convertableIdentifier">http://www.facebook.com</span>

上面的代码未经测试,但希望能够引导您进入正确的方向。

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:

<script type="text/javascript">

    $('.convertableIdentifier').each(function(i, el) {

        // grab the url and the link text
        var url = $(el).html();

        // create a new node with query by decorating a
        // empty a tag
        var newNode = $('<a></a>').attr('href', url).attr('target', '_blank').html(url);

        // replace the current node with our new node
        $(el).replaceWith(newNode);

    });

</script>

The HTML:

<span class="convertableIdentifier">http://www.google.com</span>
<span class="convertableIdentifier">http://www.youtube.com</span>
<span class="convertableIdentifier">http://www.facebook.com</span>

The above code is not tested, but should hopefully lead you in the right direction.

千仐 2024-10-27 08:27:49

使用 jQuery。

给 span 一个 id:

<span id="linkChange">http://domain.com</span>

jQuery 代码:

var href = jQuery('#linkChange').html();
var link = "<a href='"+href+"' target='_blank'>"+href+"</a>";

jQuery('#linkChange').replaceWith(link);

Use jQuery.

give the span an id:

<span id="linkChange">http://domain.com</span>

jQuery code:

var href = jQuery('#linkChange').html();
var link = "<a href='"+href+"' target='_blank'>"+href+"</a>";

jQuery('#linkChange').replaceWith(link);
吐个泡泡 2024-10-27 08:27:49

将跨度设置为 id,然后您可以执行以下操作:

var linkText = $("#yourspanid").text();

$("<a/>").attr({"href": linkText, "target": "_blank"}).text(linkText).appendTo("body");
$("#yourspanid").remove();

根据您的编辑进行更改

var elems = $("span.myClass > span");
elems.each(function(){
    var linkText= $(this).text();
    $("<a/>").attr({"href": linkText, "target": "_blank"}).text(linkText).appendTo("body");
});
elems.remove();

查看工作演示

Put the span an id and then you can do something like

var linkText = $("#yourspanid").text();

$("<a/>").attr({"href": linkText, "target": "_blank"}).text(linkText).appendTo("body");
$("#yourspanid").remove();

Changing according to your edit

var elems = $("span.myClass > span");
elems.each(function(){
    var linkText= $(this).text();
    $("<a/>").attr({"href": linkText, "target": "_blank"}).text(linkText).appendTo("body");
});
elems.remove();

See a working demo

时间你老了 2024-10-27 08:27:49

也许是这样的:(不需要知道 ids/classes)使用 jquerys for-each 循环并特定于 tds

内部的目标范围

$(文档).ready(函数(){
$('td span').each(function(){
$(this).text("" +
$(this).text() + "");
});
});

编辑:这段代码效果更好:

<script type="text/javascript">
    $(document).ready(function(){
        $('td span').each(function(){
            $(this).html("<a href='" + $(this).html() + "' />" + 
                $(this).html() + "</a>");
        });
    });
</script>

原版使用了 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:

<script type="text/javascript">
    $(document).ready(function(){
        $('td span').each(function(){
            $(this).html("<a href='" + $(this).html() + "' />" + 
                $(this).html() + "</a>");
        });
    });
</script>

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文