如何在 jQuery 中放置 target="_blank"?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1933501/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 12:26:03  来源:igfitidea点击:

How to put target="_blank" in jQuery?

jquerydom

提问by elmas

I tried many variations to put target="_blank"in links with jQuery, but I can't get it to work.

我尝试了许多变体来添加target="_blank"与 jQuery 的链接,但我无法让它工作。

Here's my code:

这是我的代码:

var thumbfile = "<?php echo $smit_iturl_v; ?>";
jQuery(document).ready(function () {
    var actualHost = jQuery.trim(jQuery.url.attr("host"));
    jQuery("a img").each(function (i) {

        if (jQuery.trim(jQuery.url.setUrl(jQuery(this).attr("src")).attr("host")) == actualHost &&      
            (jQuery.url.setUrl(jQuery(this).attr("src")).attr("path")).indexOf("wp-content") != -1 &&

            isImage(jQuery.url.setUrl(jQuery(this).attr("src")).attr("file"))) {

            var parentTag = jQuery(this).parent().get(0).tagName;
            parentTag = parentTag.toLowerCase();

            if (parentTag == "a" &&
            jQuery.url.setUrl(jQuery(this).parent().attr("href")).attr("host") == actualHost &&
            jQuery.url.setUrl(jQuery(this).parent().attr("href")).attr("path").indexOf("wp-content") != -1 &&
            isImage(jQuery(this).parent().attr("href"))) {

                var description = (jQuery(this).attr("alt") == "") ? jQuery(this).attr("title") : jQuery(this).attr("alt");
                jQuery(this).parent().attr("href", thumbfile +
                        "?title=" + jQuery(this).attr("title") +
                        "&description=" + description +
                        "&url=" + stripDomain(jQuery(this).parent().attr("href"))

                );
            }
        }
    });

How can I do it?

我该怎么做?

回答by Chetan Sastry

Too much information! This should work fine:

太多信息!这应该可以正常工作:

$("a").attr("target","_blank");

See the example here http://jsbin.com/evexi/edit. It works perfectly.

请参阅此处的示例http://jsbin.com/evexi/edit。它完美地工作。

回答by CMS

Since you are iterating over the image elements that are childs of an anchor, at the beginning of the loop you can set it:

由于您正在迭代作为锚点子元素的图像元素,因此在循环开始时您可以设置它:

//...
jQuery("a img").each(function (i) {
  // 'this' is the img element, you should get the parent anchor
  jQuery(this).parent().attr('target', '_blank'); 
  //...
});

回答by JonnyRoboto

To add to the awesome simplicity of this answer, which was super helpful by the way, you can target multiple types of links for example:

为了增加这个答案的简单性,顺便说一下,这非常有用,您可以定位多种类型的链接,例如:

$("#whatever_id a, #another_id, #yet_another, #etc").attr("target","_blank");

Just keep the different id's separated by commas.

只需用逗号分隔不同的 id。

回答by user2066719

You could give all the links you want to open in a new window a class, eg 'target-blank';

您可以为所有要在新窗口中打开的链接指定一个类,例如“target-blank”;

<a href='#' class='any existing classes target-blank'>Opens in a new window</a>

then add a drop of jQuery

然后添加一滴jQuery

$("a.target-blank").attr('target','_blank');

valid xhtml and a DOM that understands target='_blank'!

有效的 xhtml 和理解 target='_blank' 的 DOM!