简体   繁体   English

如何打开通过javascript或jQuery替换新选项卡中的链接

[英]How open replaced link in a new tab via javascript or jQuery

As you can see the below code - i am using it to replace a link on the page via javascript. 正如您可以看到下面的代码 - 我正在使用它来通过javascript替换页面上的链接。 All I want is that when replaced link is clicked ie https://boxoffice.com in this case - it should open in a new tab... 我想要的是,当点击替换链接时,即https://boxoffice.com,在这种情况下 - 它应该在新选项卡中打开...

 $(function() { var relativeUrl = "http://funmaza.com"; var menuItem = $('a[href="' + relativeUrl + '"]'); if (menuItem) { menuItem.attr("href", "https://boxoffice.com"); } }) 

To achieve what you require you can add a target attribute to the a element. 要实现所需,可以向a元素添加target属性。 Also note that the if is redundant as jQuery objects are very tolerant. 另请注意, if是多余的,因为jQuery对象非常宽容。 They will not error if you call a method on an object which holds no DOM elements. 如果在不包含DOM元素的对象上调用方法,则它们不会出错。 Try this: 尝试这个:

$(function() {
  $('a[href="http://funmaza.com"]').attr({
    href: 'https://boxoffice.com',
    target: '_blank'
  });  
});

你可以尝试一下。

$(document).find("a[href="http://funmaza.com"]").attr({href:'https://boxoffice.com'});

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

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