我有一个jQuery片段,可以打开一个新窗口中的所有出站链接。
jQuery(document.links)
.filter(function() {
return this.hostname != window.location.hostname;
})
.attr('target', '_blank');效果很好。
我试图使这种方法不适用于类.render元素中出现的任何链接(可能是也可能不是直接的父级)。
我试过:
jQuery(document.links)
.filter(function() {
return this.hostname != window.location.hostname;
})
.not( this.parents('.render'))
.attr('target', '_blank');这不仅不起作用,控制台还说parents不是一个函数。
发布于 2016-01-12 22:32:44
var renderLinks = jQuery(".render a");
jQuery(a).filter(...).not(renderLinks).attr('target', function(){ return '_blank';});发布于 2016-01-12 22:41:12
因为您已经在使用一个筛选器,所以不需要在not()中添加另一个过滤器。只需在filter()中添加附加条件即可
jQuery('a').filter(function() {
var hostMatch = this.hostname != window.location.hostname,
isRenderItem = $(this).closest(".render").length;
return hostMatch && !isRenderItem;
}).attr('target', '_blank');https://stackoverflow.com/questions/34754966
复制相似问题