简体   繁体   English

如何在.html()中添加链接

[英]How to add a link within a .html()

I am using a .click function to switch out text based on menu clicks. 我正在使用.click函数根据菜单单击切换出文本。 (example: http://jsfiddle.net/3r9hcz3z/ ) It works great except that I am still pretty new to Jquery & I can't figure out how to add a link inside of on one of the .html() menu items. (示例: http : //jsfiddle.net/3r9hcz3z/ )很好用,但是我对Jquery还是很陌生,而且我不知道如何在.html()菜单项之一中添加链接。 。

Any help would be awesome. 任何帮助都是极好的。 I know that there is a way to do it within the Jquery but just can't find the right coding. 我知道在Jquery中可以做到这一点,但是找不到正确的编码。 Thanks! 谢谢!

$('#menuone').click(function(){
$('#title').text("ONE");
$('#text').html("This is text & where I would like to be able to add a link");
});
$('#text').html("<a href='someUrl'>Link</a>");

这应该做。

一种干净的方法是将一个类添加到锚,然后通过.attr设置其href属性:

$('.a_class').attr('href','http://example.com');

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM