开发者

javascript variable value as a link

I ha开发者_StackOverflow中文版ve a variable and I would like to add a link for its value. This below example only returns the html. How can I make this a clickable link when its returned?

var link = 'new link: <a href="http://test.com/" target="_blank"><b>Clink Here</font></b></a>'


This should help you:

var myDiv = document.createElement("div");
var link = 'new link: <a href="http://test.com/" target="_blank"><b>Clink Here</font></b></a>'
myDiv.innerHTML = link;
document.body.appendChild(myDiv);

For the id:

myDiv.id = "myLink";
alert(document.getElementById('myLink').innerHTML);


You need a container where the HTML will be placed. If you can use jQuery, this becomes very easy.

<div id="container"></div>

var link = '...';
$('#container').html(link);

Working sample: http://jsfiddle.net/mEjUg/


In jQuery:

$("<div />")
    .html('new link: <a href="http://test.com/" target="_blank"><b>Click Here</b></a>')
    .appendTo("body");
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜