我尝试了许多变体,以便将target="_blank"与jQuery链接起来,但我不能让它工作。
下面是我的代码:
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"))
);
}
}
});我该怎么做呢?
发布于 2009-12-20 01:15:43
信息太多了!这应该可以很好地工作:
$("a").attr("target","_blank");请参阅此处的示例http://jsbin.com/evexi/edit。它工作得很完美。
发布于 2009-12-20 01:13:20
由于您正在迭代作为锚点的孩子的图像元素,因此您可以在循环开始时设置它:
//...
jQuery("a img").each(function (i) {
// 'this' is the img element, you should get the parent anchor
jQuery(this).parent().attr('target', '_blank');
//...
});发布于 2011-11-20 01:56:55
为了增加这个答案的简单性,顺便说一句,这非常有用,你可以针对多种类型的链接,例如:
$("#whatever_id a, #another_id, #yet_another, #etc").attr("target","_blank");只需用逗号分隔不同的id即可。
https://stackoverflow.com/questions/1933501
复制相似问题