我有一些 jQuery 代码,可以很好地在新窗口中打开外部链接,尽管我遇到了 Joomla 网站和 jQuery 的兼容性问题。我过去遇到过这个问题,解决它的最简单方法是使用 Joomla 系统 Mootools 库。
这是我需要转换为 Mootools 的 jQuery 脚本:
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function() {
$('a[href^="http://"]').filter(function() {return this.hostname && this.hostname !== location.hostname;}).attr('target', '_blank');
});
</script>
有人可以帮我转换上面的代码吗?
再次,Mootools 已通过系统插件启用。
最佳答案
如果您可能想再次选择外部链接,您可以设置一个新的伪选择器
Slick.definePseudo('external', function() {
return this.hostname && this.hostname != window.location.hostname;
});
document.getElements('a[href^=http://]:external').set('target', '_blank');
或者就像 jQuery 所做的那样。
document.getElements('a[href^=http://]').filter(function(a) {
return a.hostname && a.hostname != window.location.hostname
}).set('target', '_blank');
关于javascript - 将 jQuery 转换为 Mootools,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4975124/