Tôi đã thử nhiều biến thể để đặt target="_blank"
trong các liên kết với jQuery, nhưng tôi không thể làm cho nó hoạt động.Làm thế nào để đặt target = "_ blank" trong jQuery?
Dưới đây là mã của tôi:
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"))
);
}
}
});
Làm thế nào tôi có thể làm điều đó?
Nếu bạn chỉ muốn mở các liên kết bên ngoài trong một cửa sổ mới thì regex này có thể hữu ích. '$ (" a [href^= 'http'] "). attr ('target', '_ blank');' –
cảm ơn bạn đời, đã giúp tôi quá – DextrousDave
Điên hữu ích, cảm ơn! –