[英]Adapting javascript function to open link in new tab
Is there a way of adapting the following javascript to either open the link below in a new browser tab or window? 有没有一种方法可以改编以下javascript,以在新的浏览器标签或窗口中打开下面的链接?
document.getElementsByClassName("yellow")[0].click();
Link on page looks like this: <a class="yellow">Buy</a>
页面上的链接如下所示:
<a class="yellow">Buy</a>
The javascript needs to be entered in a plugin. javascript需要在插件中输入。 I can't alter the code on the page itself.
我无法更改页面本身上的代码。
Maybe something like this? 也许是这样的吗?
JS: JS:
url = document.getElementById("yellow").getAttribute("href");
var win = window.open(url, '_blank');
win.focus();
If you want to load a plug in script to achieve this, you'll want to include something like this: 如果要加载插件脚本来实现此目的,则需要包含以下内容:
var el = document.getElementsByClassName("yellow")[0];
el.onclick = function () {
newTab(this.href);
};
function newTab(url) {
var win = window.open(url, '_blank');
win.focus();
}
This will add onclick listener to the element in question, and use it's href
property to open that url in a new tab/window (depending on browser settings). 这会的onclick监听器添加到有问题的元素,并使用它
href
属性在新标签/窗口中打开该网址(取决于浏览器设置)。
Pop-up blocker add-ons will probably disallow this though... 弹出窗口阻止程序加载项可能会禁止此操作...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.