从类jQuery创建超链接

从类jQuery创建超链接,jquery,html,Jquery,Html,如何使用Jquery使以下内容成为超链接? 我想让单词链接成为超链接 你好,世界!链接。您可以通过类名访问元素 $('.link') 然后使用链接标记和文本更改其html,如: $('.link').html('<a href="your_link">'+ $('.link').html() + '</a>'); 使用jQuery,您可以通过 $(document).ready(function(){ var linkClass = $(document).f

如何使用Jquery使以下内容成为超链接? 我想让单词链接成为超链接


你好,世界!链接。

您可以通过类名访问元素

$('.link')
然后使用链接标记和文本更改其html,如:

$('.link').html('<a href="your_link">'+ $('.link').html() + '</a>');

使用jQuery,您可以通过

$(document).ready(function(){
    var linkClass = $(document).find(".link");
    var linkClassHTML = linkClass.html();
    linkClass.html("");
    $(linkClass).append("<a href='#>"+linkClassHTML+"</a>");

});

使用jQuery,您可以简单地使用以下代码,这些代码不会与任何其他标记冲突:

// Create an anchor tag pointing to the required URL if needed having the .link contents
const anchorTag = $('<a>').attr('href', 'http://EXAMPLE.com').html( $('p > .link').html() );
//Getting The direct plain text of "p" tag
const pText = $('p > .link').closest('p')
     .clone()              //clone the element
     .children()           //select all children
     .remove()             //remove all children
     .end()                // back to the selected element
     .text();              //Gets the direct "p" tag text
//Replace the "p" tag contents
$('p > .link').closest('p').html(pText).append(anchorTag);

多亏了Delgan,我们可以在

上找到任何标记的直接纯文本。如果您是指带有url的可单击超链接,那么您应该更改标记。如果您指的是指针光标,那么可以使用css cursor:pointer。