我正在试图弄清楚如何编写一个“切换语句”,这样如果一个特定的(让我们称之为'yourdomain.com')外部URL匹配,它不会在新窗口中打开,其余的就会在新窗口中打开。有人能帮帮忙教我吗?
jQuery(function ($) {
$('a[href^="http://"]')
.not('[href*="http://mydomain.com"]')
.attr('target', '_blank');例如:
mydomain.com =这将不会打开新窗口,因为它是我的URL
yourdomain.com =即使是外部URL,此特定URL也不会在新窗口中打开。
anyotherdomain.com =除上述网址外,其他所有外部网址都将在新窗口中打开
发布于 2012-10-26 10:18:01
通过添加另一个.not()解决了这个问题
jQuery(function ($) {
$('a[href^="http://"]')
.not('[href*="http://mydomain.com"]').not('[href*="http://yourdomain.com"]')
.attr('target', '_blank');发布于 2012-10-26 09:57:22
$(function() {
var elms = $('a[href^="http://"]');
$.each(elms, function() {
var current_link = $(this).attr("href");
if (current_link == "yoururl") {
$(this).attr('target', '_blank');
}
});
});这会有帮助吗?
https://stackoverflow.com/questions/13079749
复制相似问题