覆盖内置JavaScript对象的成员

时间:2022-06-13 11:00:47

I would like to override the default behavior of the offsetParent member of all html elements. I would like to do something like this if it's possible:

我想覆盖所有ht​​ml元素的offsetParent成员的默认行为。如果有可能,我想做这样的事情:

// For <div id="foo"></div>
var oFooElem = document.getElementById("foo");
oFooElem._offsetParent = oFooElem.offsetParent;
oFooElem.prototype.offsetParent = function() {
    window.status = 'offsetParent called.';
    return this._offsetParent;
};

Is this even possible? If so, please post the corrected code sample. Thanks!

这有可能吗?如果是,请发布更正后的代码示例。谢谢!

Update:

@some Thank you for your help. It sounds like this is not possible. I have expanded on the problem that provoked this question in a new question if you're interested.

@some感谢您的帮助。听起来这是不可能的。如果你感兴趣,我已经在一个新问题中扩展了引发这个问题的问题。

1 个解决方案

#1


3  

As far as I know it is not possible. In firefox I get exception "setting a property that only has a getter", IE gives error, Chrome gives error, but in Opera it works (if I assign it on the element, not the prototype)!

据我所知,这是不可能的。在Firefox中我得到异常“设置一个只有一个getter的属性”,IE给出错误,Chrome给出错误,但在Opera中它可以工作(如果我在元素上分配它,而不是原型)!

But you have another problem. offsetParent isn't a function so it will never be called like a function and your alert will never happen. In opera (the only browser I found where you could replace it in) you only get the function back.

但是你有另一个问题。 offsetParent不是一个函数,所以永远不会像函数一样调用它,你的警报永远不会发生。在opera(我发现你可以替换它的唯一浏览器)中,你只能恢复功能。

You can however add a new function (will work in Firefox, Chrome and Opera but not in IE since IE don't have the constructor property):

但是你可以添加一个新功能(可以在Firefox,Chrome和Opera中使用,但在IE中没有,因为IE没有构造函数属性):

var e = document.getElementById("mydiv"); //Get a div element
//Adds a _offsetParent function to all DIV elements.
e.constructor.prototype._offsetParent=function(){
  alert("offset parent called"); return this.offsetParent;
};
e._offsetParent();

Update By reading your description here I see your error:

更新通过阅读您的说明,我看到您的错误:

  1. you get a reference to the object called myInnerContent
  2. 你得到一个名为myInnerContent的对象的引用

  3. you remove that object from the DOM when you replace the content of the outer tag.
  4. 替换外部标记的内容时从DOM中删除该对象。

  5. you try to get the parent from the old object who is orphan and that no longer are in the DOM. IE gives you an Error and Firefox gives you null.
  6. 你试图从孤儿的旧对象中获取父级,并且不再在DOM中。 IE给你一个错误,Firefox给你null。

You already have the solution on your page: Save the name of the object. Optionally you can choose to update the global variable when you update the content, or only update the innerHTML of the inner div.

您已在页面上拥有解决方案:保存对象的名称。 (可选)您可以选择在更新内容时更新全局变量,或仅更新内部div的innerHTML。

#1


3  

As far as I know it is not possible. In firefox I get exception "setting a property that only has a getter", IE gives error, Chrome gives error, but in Opera it works (if I assign it on the element, not the prototype)!

据我所知,这是不可能的。在Firefox中我得到异常“设置一个只有一个getter的属性”,IE给出错误,Chrome给出错误,但在Opera中它可以工作(如果我在元素上分配它,而不是原型)!

But you have another problem. offsetParent isn't a function so it will never be called like a function and your alert will never happen. In opera (the only browser I found where you could replace it in) you only get the function back.

但是你有另一个问题。 offsetParent不是一个函数,所以永远不会像函数一样调用它,你的警报永远不会发生。在opera(我发现你可以替换它的唯一浏览器)中,你只能恢复功能。

You can however add a new function (will work in Firefox, Chrome and Opera but not in IE since IE don't have the constructor property):

但是你可以添加一个新功能(可以在Firefox,Chrome和Opera中使用,但在IE中没有,因为IE没有构造函数属性):

var e = document.getElementById("mydiv"); //Get a div element
//Adds a _offsetParent function to all DIV elements.
e.constructor.prototype._offsetParent=function(){
  alert("offset parent called"); return this.offsetParent;
};
e._offsetParent();

Update By reading your description here I see your error:

更新通过阅读您的说明,我看到您的错误:

  1. you get a reference to the object called myInnerContent
  2. 你得到一个名为myInnerContent的对象的引用

  3. you remove that object from the DOM when you replace the content of the outer tag.
  4. 替换外部标记的内容时从DOM中删除该对象。

  5. you try to get the parent from the old object who is orphan and that no longer are in the DOM. IE gives you an Error and Firefox gives you null.
  6. 你试图从孤儿的旧对象中获取父级,并且不再在DOM中。 IE给你一个错误,Firefox给你null。

You already have the solution on your page: Save the name of the object. Optionally you can choose to update the global variable when you update the content, or only update the innerHTML of the inner div.

您已在页面上拥有解决方案:保存对象的名称。 (可选)您可以选择在更新内容时更新全局变量,或仅更新内部div的innerHTML。