passing a target to the js link() method

孤者浪人 提交于 2019-12-14 03:53:26

问题


How do I pass a target, _blank, to the js link method?

x = "my link to google"
x.link("www.google.com")

<a href="www.google.com">my link to google</a>

if its not possible is there an alternative method I could use?


回答1:


You can't; the String.link method is ancient and mostly deprecated. Construct the link using DOM methods instead:

var link = document.createElement("a");
link.setAttribute("href", "http://www.google.com/");
link.setAttribute("target", "_blank");
link.appendChild(document.createTextNode("my link to google"));
...
// this, or whatever else you want to do to add it to the document:
document.getElementById("something").appendChild(link);

Either that, or just build up the string yourself. String.link isn't doing much anyway.



来源:https://stackoverflow.com/questions/8424505/passing-a-target-to-the-js-link-method

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!