具有Mootools的target_blank链接的PB [英] Pb with a target_blank link with Mootools

查看:102
本文介绍了具有Mootools的target_blank链接的PB的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Joomla 1.6架构中,我想使用window.location.hostname将target_blank应用于所有外部链接.

within a Joomla 1.6 architecture, I want to apply a target_blank to all external links with the use of window.location.hostname.

我的代码(不是很漂亮)是: http://jsfiddle.net/Y54Me/

My (not very pretty) code is thus : http://jsfiddle.net/Y54Me/

但是可以看出,这不适用于我想要的某种href,例如href ="javascript:;".

But as can be seen, this doesn't apply as I want to certain kind of href, like a href="javascript:;".

很高兴得到任何建议.

TKS.

推荐答案

如果本地链接不包含域,则可以通过CSS2定位它们:

if your links locally do not contain the domain, you can just target them via CSS2:

a[href^="http://"], a[href^="https://"], a[href^='javascript'], a[href^='#'] {
    background: url('http://code.google.com/webtoolkit/tools/gwtdesigner/userinterface/images/globe3.png') 100% 60% no-repeat;
    padding:  0 25px 0 0;
}

并通过选择器为$$进行匹配:

and matching that via a selector for $$:

// 1.12
$$("a[href^='http://'], a[href^='https://'], a[href^='javascript'], a[href^='#']").addClass("external");

// or for mootools 1.2.5+ 
document.getElements("a[href^='http://'], a[href^='https://'], a[href^='javascript'], a[href^='#']").addClass("external").addEvent("click", somefunc);

不需要循环,正则表达式,字符串操作等.

no looping, regex, string manipulations etc required.

否则,您可以通过以下方法过滤上述结果:

otherwise, you can filter the result of the above via:

var hostname = "jsfiddle.net";

$$("a[href^='http://'], a[href^='https://'], a[href^='javascript'], a[href^='#']").filter(function(link) {
    return !link.get("href").contains(hostname);
}).addClass("external");

http://www.jsfiddle.net/dimitar/Y54Me/1/ http://www.jsfiddle.net/dimitar/Y54Me/2/为1.12(.getProperty而不是.get)

http://www.jsfiddle.net/dimitar/Y54Me/1/ and http://www.jsfiddle.net/dimitar/Y54Me/2/ for 1.12 (.getProperty instead of .get)

最后.而不是将单击事件附加到它们上,然后将它们附加到window.open上,为什么不只使用.set("target", "_blank")呢?这是imo中最语义和最简洁的方法.

finally. rather than attach click events to them and then window.open, why not just do .set("target", "_blank") instead? that's the most semantic and clean approach imo.

这篇关于具有Mootools的target_blank链接的PB的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆