Javascript jquery插件-正在工作,但在调试时,结果甚至在到达该行之前就显示出来了
我的jquery插件脚本:Javascript jquery插件-正在工作,但在调试时,结果甚至在到达该行之前就显示出来了,javascript,jquery-plugins,coffeescript,Javascript,Jquery Plugins,Coffeescript,我的jquery插件脚本: (($) -> $.fn.externalify = (options) -> console.log "start" console.log @ settings = $.extend { 'rel' : 'external' } , options links = $ @selector+" a" console.log "---first links--" co
(($) ->
$.fn.externalify = (options) ->
console.log "start"
console.log @
settings = $.extend {
'rel' : 'external'
} , options
links = $ @selector+" a"
console.log "---first links--"
console.log links
console.log '---first links--'
if links.length is 0
if ((@[0].toString().indexOf("http://") is 0) or (@[0].toString().indexOf("https://") is 0))
unless @[0].host is window.location.host
$(@).attr settings
console.log "---links---"
console.log links
console.log '----links---'
for i in links
if (i.toString().indexOf("http://") is 0)or(i.toString().indexOf("https://") is 0)
console.log "--"
console.log i
console.log "--"
unless i is window.location.host
console.log "Before : "
console.log $(i)
$(i).attr settings
console.log "After : "
console.log $(i)
) jQuery
javascript代码:
(function() {
(function($) {
return $.fn.externalify = function(options) {
var i, links, settings, _i, _len, _results;
console.log("start");
console.log(this);
settings = $.extend({
'rel': 'external'
}, options);
links = $(this.selector + " a");
console.log("---first links--");
console.log(links);
console.log('---first links--');
if (links.length === 0) {
if ((this[0].toString().indexOf("http://") === 0) || (this[0].toString().indexOf("https://") === 0)) {
if (this[0].host !== window.location.host) $(this).attr(settings);
}
}
console.log("---links---");
console.log(links);
console.log('----links---');
_results = [];
for (_i = 0, _len = links.length; _i < _len; _i++) {
i = links[_i];
if ((i.toString().indexOf("http://") === 0) || (i.toString().indexOf("https://") === 0)) {
console.log("--");
console.log(i);
console.log("--");
if (i !== window.location.host) {
console.log("Before : ");
console.log($(i));
$(i).attr(settings);
console.log("After : ");
_results.push(console.log($(i)));
} else {
_results.push(void 0);
}
} else {
_results.push(void 0);
}
}
return _results;
};
})(jQuery);
}).call(this);
(函数(){
(函数($){
return$.fn.externalify=函数(选项){
变量i、链接、设置、\u i、\u len、\u结果;
控制台日志(“启动”);
console.log(this);
设置=$.extend({
“rel”:“外部”
},选项);
links=$(this.selector+“a”);
log(“--first links--”;
控制台日志(链接);
log('--first links--');
如果(links.length==0){
if((此[0].toString().indexOf(“http://”)==0)| |(此[0].toString().indexOf(“https://”)==0)){
if(this[0].host!==window.location.host)$(this.attr(settings);
}
}
console.log(“--links--”;
控制台日志(链接);
log('----链接---');
_结果=[];
对于(_i=0,_len=links.length;_i<_len;_i++){
i=链接[_i];
if((i.toString().indexOf(“http://”)==0)| |(i.toString().indexOf(“https://”)==0)){
console.log(“”);
控制台日志(i);
console.log(“”);
if(i!==window.location.host){
console.log(“之前:”);
console.log($(i));
$(i).attr(设置);
console.log(“之后:”);
_结果.push(console.log($(i));
}否则{
_结果:推挤(无效0);
}
}否则{
_结果:推挤(无效0);
}
}
返回结果;
};
})(jQuery);
}).打电话(这个);
如果有这样的链接
<span><a href="http://google.com">google</a></span> in body tag .
在body标签中。
如果我运行$(“span”).externalify(),那么属性“rel”:“external”将添加到链接中,从而使链接成为:
<span><a rel="external" href = "http://google.com">google</a></span>
它工作得很好,但是如果你看到我的代码,我已经放了很多console.log命令。所有人都在用“rel”打印新的:“external”,在手术前后,它都显示了磨损,怎么会发生这种情况 既然你可以简化,为什么还要困惑?看看我在这里创建的代码:它工作得很好。。。希望能有所帮助。既然你可以简化,为什么还要困惑呢?看看我在这里创建的代码:它工作得很好。。。希望能有所帮助。这是一大堆代码,基本上是这样的:
$('a').filter(->
/^http/.test(this.href) and this.href.indexOf(location.host) < 0
).attr(rel: 'external')
$('a')。过滤器(->
/^http/.test(this.href)和this.href.indexOf(location.host)<0
).attr(rel:“外部”)
代替插件,您可以考虑为外部链接实现自定义伪选择器:
$.expr[':']['external'] = (elem) ->
/^(\w+:)?\/\//.test(elem.href) and elem.href.indexOf(location.host) < 0
// usage:
$('.container a:external').attr('rel', 'external')
$.expr[':']['external']=(elem)->
/^(\w+:)?\/\/.test(elem.href)和elem.href.indexOf(location.host)<0
//用法:
$('.container a:external').attr('rel','external'))
注意,我将简单的“http”测试替换为更复杂的正则表达式;省略协议的URL(如
//google.com
)是允许的,并且越来越常见。仅在开始时出现/
,就足以检测到绝对url。这还捕获了除http之外的任何其他协议。这是大量的代码,基本上是这样的:
$('a').filter(->
/^http/.test(this.href) and this.href.indexOf(location.host) < 0
).attr(rel: 'external')
$('a')。过滤器(->
/^http/.test(this.href)和this.href.indexOf(location.host)<0
).attr(rel:“外部”)
代替插件,您可以考虑为外部链接实现自定义伪选择器:
$.expr[':']['external'] = (elem) ->
/^(\w+:)?\/\//.test(elem.href) and elem.href.indexOf(location.host) < 0
// usage:
$('.container a:external').attr('rel', 'external')
$.expr[':']['external']=(elem)->
/^(\w+:)?\/\/.test(elem.href)和elem.href.indexOf(location.host)<0
//用法:
$('.container a:external').attr('rel','external'))
注意,我将简单的“http”测试替换为更复杂的正则表达式;省略协议的URL(如
//google.com
)是允许的,并且越来越常见。仅在开始时出现/
,就足以检测到绝对url。这也会捕获http之外的任何其他协议。嘿,你能提供编译好的js吗…不擅长CoffeeScription你会多次调用吗$。externalify
?@Alexander多次调用意味着,这是我们的愿望,我们可以调用任意次数,但我认为这是第一次,在开始时,它应该显示而不添加attributes@rameshkumar,到处都是。你的意思是,在第一次之后,它应该忽略已经处理的链接?好的…还有一件事…插件是如何调用的?我是说什么元素?你能举个例子吗?嘿,你能提供编译好的js吗…不擅长咖啡脚本你会多次调用$。externalify
?@Alexander多次调用意味着,这是我们的愿望,我们可以调用任意次数,但我认为,这是第一次,在开始时它应该显示出来,而不添加attributes@rameshkumar,到处都是。你的意思是,在第一次之后,它应该忽略已经处理的链接?好的…还有一件事…插件是如何调用的?我是说什么元素?你能举个例子吗?outerHTML
带RegEx吗?我希望你是在开玩笑。为什么?怎么了?你检查过链接了吗?它们可以工作…而正则表达式,据我所知,可以工作得很好…我特别注意额外/无空格…如果你能告诉我可能出现的潜在问题,然后,我可以提议切割和组装计算机程序中数据或指令符号排列的组件,或一组这样的指令——我可以编辑它。:)不需要使用outerHTML
,函数应该使用this.href
。有效链接
失败了。我以前认为这样可以。但是它的问题是this.href返回一个完整的url——如果您使用RegEx使用outerHTML
?我希望你是在开玩笑。为什么?怎么了?你检查过链接了吗?它们可以工作…而正则表达式,据我所知,可以工作得很好…我特别注意额外/无空格…如果你能告诉我可能出现的潜在问题,然后,我可以提议切割和组装计算机程序中数据或指令符号排列的组件,或一组这样的指令——我可以编辑它。:)不需要outerHT