我试图为NodeList对象定义一个函数。这是代码:添加函数到NodeList对象在Firefox中不起作用
if (!NodeList.prototype.filter){
NodeList.prototype.filter = function(fun /*, thisp*/){
var len = this.length;
if (typeof fun != "function")
throw new TypeError();
var res = new Array();
var thisp = arguments[1];
for (var i = 0; i < len; i++){
if (i in this){
var val = this[i]; // in case fun mutates this
if (fun.call(thisp, val, i, this))
res.push(val);
}
}
return res;
};
}
这适用于Chrome,但不适用于Firefox。萤火虫说,“current_node.childNodes.filter是不是一个函数”当我打电话的功能:
nodes = current_node.childNodes.filter(filterByClass);
奇怪的是,这种代码:
if(typeof NodeList.prototype.filter == 'function')
alert(NodeList.prototype.filter);
显示功能,在这两个浏览器的代码。
它在HTML中使用,并且它被纳入这样的:
<script type="text/javascript" src="textselection.js"></script>
编辑: Firefox的版本是10.0.2和S.O. Ubuntu的11.04
EDIT2: 我已经忘记了一个重要的因素......它是被用来whithin一个iframe
适合我。 – 2012-03-01 16:45:26
我也是:http://jsfiddle.net/Ewupb/ – gilly3 2012-03-01 16:46:00