November 1, 2007
jQuery 之 没有 ‘target’ 属性照样打开新窗口(续)
看到有朋友需要修改之后的 Alexa 转向插件,由于修改简单并且此功能并不属于原插件,所以我没有另发新版,需要修改的朋友请看下面的方法:(基于 Arctic's Internal Alexa Redirect 1.1 版)
我们只需要修改 wp_lzw_parse_external_links 函数的一行代码,直接复制下面的函数将插件的相应部分替换掉就可以了:
if ( wp_lzw_get_domain_name_from_uri($matches[3]) == wp_lzw_get_domain_name_from_uri($_SERVER["HTTP_HOST"]) && wp_lzw_exclude($matches[3]) == true ) {
return '<a href="' . $matches[2] . '//' . $matches[3] . '"' . $matches[1] . $matches[4] . ' onclick="parent.location.href=' . '\'' . 'http://redirect.alexa.com/redirect?' . $matches[2] . '//' . $matches[3] . '\'' . ';return false;"' . '>' . $matches[5] . '</a>';
}
else {
return '<a href="' . $matches[2] . '//' . $matches[3] . '"' . $matches[1] . $matches[4] . ' rel="external"' . '>' . $matches[5] . '</a>';
}
}
最关键的部分在于第 6 行的 rel="external",jQuery 将带有此标签的链接进行处理,详见上一篇。
不知道细心的朋友发现没有,其实这样做会产生一个 bug,被 wp_lzw_exclude 函数排除在外的内部链接也会被打上 external 标记。不过由于我设置过滤包含有 /wp-admin/ 的链接,而刚好它们是 WP 的后台链接,从新窗口打开其实更方便些,我就懒得改了。
update: 要解决 underone 在下面留言中提出的问题还是得改一下插件,顺便把上面的这个 bug 也一并修复了。用下面的代码覆盖原有函数,然后把你不想添加 rel="external" 标签的链接的特征串添加到 wp_lzw_exclude 函数的排除数组中。
if ( wp_lzw_exclude($matches[3]) == true ) {
if ( wp_lzw_get_domain_name_from_uri($matches[3]) == wp_lzw_get_domain_name_from_uri($_SERVER["HTTP_HOST"]) ) {
return '<a href="' . $matches[2] . '//' . $matches[3] . '"' . $matches[1] . $matches[4] . ' onclick="parent.location.href=' . '\'' . 'http://redirect.alexa.com/redirect?' . $matches[2] . '//' . $matches[3] . '\'' . ';return false;"' . '>' . $matches[5] . '</a>';
} else {
return '<a href="' . $matches[2] . '//' . $matches[3] . '"' . $matches[1] . $matches[4] . ' rel="external"' . '>' . $matches[5] . '</a>';
}
} else {
return '<a href="' . $matches[2] . '//' . $matches[3] . '"' . $matches[1] . $matches[4] . '>' . $matches[5] . '</a>';
}
}

我这个废柴,艰难的成功了…………感谢!
还有一事求助
如果针对某些链接,并不想使用rel=”external”,应该如何处理呢?
比如俺blog上sidebar的technorati和something支流……
underone » 假设你只想为文章内容(假设它的 id 是 #content)里的外部链接添加 rel=”external”,那么把 jQuery 里的 a[rel=] 改为 #content > a[rel=] 就可以了。
说得不太对。刚才的方法解决不了不添加 rel=”external”,只能解决不添加 class=”external”,虽然这样也可以阻止从新窗口打开。rel 属性是用插件加上去的,还是得改一下插件本身。等下我把代码贴上来。
好的…
代码贴在上面,看到了吧?
代码白痴继续问:
$words = array (‘/wp-admin/’, ‘/tag/’, ‘#sidebar’);
这样的是不是不对
我是想使ID为#sidebar部分不添加rel属性
这样不行的,要添加链接的特征部分。比如说你想排除掉 Technorati 的链接,那就把 ‘technorati.com’ 加进去。Wait… 你仔细说一下你的目标吧,我去你那里看过了,你想去掉那个图标,还是不在新窗口打开,还是去除 rel 标签?我明确知道你想做到什么才好办。
因为这涉及到三个部分的问题:CSS, jQuery 和插件,去除图标改的是 CSS,去除新窗口打开功能在 jQuery,仅仅去除 rel 标签要改插件。
我想这样:
应该是在侧边栏去除那个图标就可以了。
也想知道在侧边栏中所有链接都不添加rel的办法
第一个问题好解决,只要在 CSS 中设置只有 #content 的外部链接才会显示图标就好了,可能是这个样子的: #content a.external{/*这里是背景图片设置等*/}
第二个问题不太好解决,因为插件处理的是页面的所有链接,而不管处于哪个 div 容器中。
呵呵,希望这样说你能明白。
部分新开不现实图标,改CSS的办法我实现了,一下没转过弯来…
谢谢耐心指导,现在这样大概够用了:)
^_^
顺便把.ZIP和.RAR也添加到规则里了:)
插件升级啦~
北极冰仔用href打开页面吧,location不能带上refer。
遇到了另外一些问题,比如某些插件用上以后,变成新开窗口的话会产生代码错误。比如这个wp_addbookmarks插件
http://www.thinkagain.cn/archives/831.html
所有订阅图标如果点击变成新开的话,代码都会出现错误
所以我想要是新开的问题可以在哪里选择专门针对哪个DIV容器就好了,哈哈
好在暂时也不用那个插件。
不需要新开窗口的链接可以过滤呀,插件要处理 DOM 不太现实,所以只能这么将就了。呵呵。