什么'这是使用jQuery将一些文本锚定到外部链接的最简单方法

What's the easiest way to anchor some text to an external link using jQuery?

本文关键字:文本 外部 链接 方法 最简单 jQuery 什么      更新时间:2023-09-26

在我的HTML页面中,我使用以下内容添加了一些文本:

<script>
    $('.description').text('my description')
</script>

my description内部,我需要放置一个指向外部链接的锚标记href,如下所示:<a href="http://link_to_website.com">link to a website</a>

我如何使用jQuery来实现这样的目标?

您可能需要这样做:

$('.description').text('my description').append($('<a />', {href:"http://linkofhref", text:"text goes here."}));

查看下面的演示:

$('.description').text('my description ').append($('<a />', {href:"http://linkofhref", text:"text goes here."}));
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class='description'></div>

试试这个。。

<script>
    $(document).ready(function(){
        $('.description').html('<a href="http://link_to_website.com">link to a website</a>');
});
</script>

use.html()

   $('.description').html('<a href="http://link_to_website.com">link to a website</a>')

$('.description').html('my description <br> <a href="http://link_to_website.com">link to a website</a>')