将目标传递给js link()方法 [英] passing a target to the js link() method

查看:109
本文介绍了将目标传递给js link()方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何将目标_blank传递给js link 方法?

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?

推荐答案

你不能; String.link 方法很古老,大部分已弃用。使用DOM方法构造链接:

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);

或者只是自己构建字符串。 String.link 无论如何都没有做太多。

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

这篇关于将目标传递给js link()方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆