在 JQuery 中为文本添加超链接

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18128504/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 21:04:03  来源:igfitidea点击:

Adding Hyperlink to text in JQuery

jqueryhyperlinkinnertext

提问by Parag Srivastava

I am using innerText to add text to my object. Is there an easy way to add a hyperlink to the text? 'trend' also has a attribute called 'link'.

我正在使用 innerText 向我的对象添加文本。有没有一种简单的方法可以为文本添加超链接?'trend' 也有一个名为 'link' 的属性。

this.node.innerText = trend.get('value');

回答by Vaibs_Cool

Use **WRAP**function

使用**WRAP**功能

  $(someSelector).wrap(function() {
       var link = $('<a/>');
       link.attr('href', 'somewhere_far_far_away');
       link.text($(this).text());
       return link;
    });

回答by Diodeus - James MacFarlane

You need to add a DOM element using jQuery's wrap():

您需要使用 jQuery 的 wrap() 添加一个 DOM 元素:

$(this).wrap('<a href="..." />');

$(this).wrap('<a href="..." />');