简体   繁体   English

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

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

inside my HTML page I add some text using this: 在我的HTML页面内,我使用以下代码添加一些文本:

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

Inside my description I need to place an anchor tag href to an external link like this: <a href="http://link_to_website.com">link to a website</a> my description我需要将锚标记href放置到这样的外部链接: <a href="http://link_to_website.com">link to a website</a>

How could I proceed using jQuery to achieve something like this? 我怎样才能继续使用jQuery来实现这样的目标?

You might need to do this: 您可能需要这样做:

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

Check the demo below: 查看下面的演示:

 $('.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> 

use .html() 使用.html()

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

or 要么

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

try this.. 尝试这个..

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 将文件链接到 Javascript 程序的最简单方法是什么? - What's the easiest way to link a file to a Javascript program? jQuery外部链接锚点动画 - jQuery external link anchor animate 从当前页面的位置解析锚点的最简单方法是什么? - What's the easiest way to parse the anchor out of the current page's location? 将Flash添加到可与Jquery一起使用的html页面的最简单方法是什么? - What's the easiest way to add flash to an html page to use with Jquery? 对 jQuery 进行表单验证的最简单方法是什么? - What's the easiest way to do form validation for jQuery? 使用laravel和js生成表单的最简单方法是什么? - What's the easiest way to generate forms using laravel and js? jQuery平滑滚动外部锚链接 - Jquery smooth scroll external anchor link 如何不使用id链接到外部网站的特定元素? (即在锚标记的href网址末尾不使用“#some-id”)? - How can I link to a specific element of an external website WITHOUT using id? (i.e. NOT using “#some-id” at the end of my anchor tag's href url)? 使用JavaScript将弹出式div放在链接/光标位置的最简单方法是什么? - What's the easiest way to put a pop up div at link / cursor location utilizing JavaScript? 什么是最简单的访问方式<li>单击的元素是通过 jQuery 内部的吗? - What's the easiest way to access the <li> an element that's clicked is within via jQuery?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM