HTMLElement.toString() 仅适用于 Chrome

发布于 2024-12-23 13:49:51 字数 1370 浏览 2 评论 0原文

我在 FireFox 和 Opera 中发现错误。(我运行的是 Debian Whezee、Firefox 9.0.1、Opera 11.60、Chrome 16.0.912.63) 当你输入这样的内容时:

var a = document.createElement('a')
a.toString = function(){ return "Hello" }
alert(a+" World");

,但在 Firefox 和 Opera(不知道 IE)中,你会得到类似这样的“[object HTMLElement] World”

在 Chrome 中,你会得到一个带有“Hello World”的警告框,正如你所期望的那样 试图解决这个问题:

Object.prototype.toString = function(){ return "Hello" };
Function.prototype.toString = function(){ return "Hello" };

它不起作用,因为 DOM 元素不是对象(不继承自对象,因此函数),甚至这也没有帮助:

HTMLElement.prototype.toString = function(){ return "Hello" };
Element.prototype.toString = function(){ return "Hello" };
Node.prototype.toString = function(){ return "Hello" };

因为 HTMLElement、Element、Node 都是接口,而不是函数。

现在我的问题是在哪里报告这个错误?有人可以帮我解决这个问题吗?

//Edit

这是我原来的 toString 函数:

var a = document.createElement('a');
a.toString = function(){
    var tmp=document.createElement('div');
    tmp.appendChild(this);
    return tmp.innerHTML;
}

正如你所看到的,这是 externalHTML 的替代函数,我需要它作为以下行中的默认行为

document.getElemntById('someID').innerHTML += "click this link:"+a;

结果应该是这样的:

<div id='someID'>click this link:<a></a></div>

I have found an error in FireFox and Opera.(I'm running Debian Whezee, Firefox 9.0.1, Opera 11.60, Chrome 16.0.912.63)
When you type something like this:

var a = document.createElement('a')
a.toString = function(){ return "Hello" }
alert(a+" World");

In Chrome you will get alert box with "Hello World" as you expect, but in firefox and opera (don't know about IE) you get something like this "[object HTMLElement] World"

What I've tried to solve this:

Object.prototype.toString = function(){ return "Hello" };
Function.prototype.toString = function(){ return "Hello" };

It doesn't work Because DOM elements are not Object (don't inherits from Object and therefor Function) and even this didn't help:

HTMLElement.prototype.toString = function(){ return "Hello" };
Element.prototype.toString = function(){ return "Hello" };
Node.prototype.toString = function(){ return "Hello" };

Because HTMLElement, Element, Node are all interfaces and not functions.

Now my question is where to report this mistake? Can someone help me with this?

//Edit

This is my original toString function:

var a = document.createElement('a');
a.toString = function(){
    var tmp=document.createElement('div');
    tmp.appendChild(this);
    return tmp.innerHTML;
}

As you can see this is alternative to outerHTML and I need this as a default behavior in following line

document.getElemntById('someID').innerHTML += "click this link:"+a;

The result should be like this:

<div id='someID'>click this link:<a></a></div>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

原谅我要高飞 2024-12-30 13:49:51

我认为没有任何地方可以报告这一点,因为在 dom 元素上调用 toString 不是标准行为。要从元素中获取 html 内容,您应该使用innerHTML,并仅获取文本内容,textContent(或旧版 IE 浏览器的innerText)

I don't think there's any place to report this since calling toString on dom elements is not standard behavior. To get the html content from an element you're supposed to use innerHTML, and to get just the text content, textContent (or innerText for older IE browsers)

东风软 2024-12-30 13:49:51

每当您尝试更改项目的默认/本机行为时;这几乎总是一个主意。我想我可能会建议:

var aa = document.createElement('a');
document.body.appendChild(aa);
var nodeList = document.getElementsByClassName('*');
for (var i = 0, len = nodeList.length; i < len; i ++) {
    nodeList[i].myCustomFunction = toStringOverride;
}
function toStringOverride() {
    return "Hello ";
}

alert(aa.myCustomFunction() + " World");

Anytime you are trying to change the default/native behavior for your project; it's almost always a bad idea. I guess I might suggest:

var aa = document.createElement('a');
document.body.appendChild(aa);
var nodeList = document.getElementsByClassName('*');
for (var i = 0, len = nodeList.length; i < len; i ++) {
    nodeList[i].myCustomFunction = toStringOverride;
}
function toStringOverride() {
    return "Hello ";
}

alert(aa.myCustomFunction() + " World");
微暖i 2024-12-30 13:49:51

数组对象有可以覆盖的toString,但为什么DOM元素没有这个

Array 是原生 JavaScript 对象。 ECMAScript 标准规定了 JS 对象如何工作的要求,包括将不同的 toString 写入原型的能力。

然而,DOM 对象没有在任何地方被指定为原生 JS 对象或遵守 JS 对象的一般行为规则。它们被允许成为标准所称的“主机对象”,而这些对象的具体规定要少得多。

破解他们的原型是非常不可靠的,而且根本不是一个好主意。尝试了一个 JS 库,prototype.js,结果混乱

请让Node继承自Object。请

抱歉,Stack Overflow 无权要求 Node 成为原生 JavaScript 对象!

在您能够说服所有浏览器供应商对 DOM 的本机 JS 对象进行标准化之前,您将必须使用外部函数 Node_getOuterHTML(node) { ... } 并显式调用它,“点击此链接:”+Node_getOuterHTML(a)

尽管如此,通常最好将您创建的节点直接附加到文档中,而不是通过 DOM->HTML-markup->DOM 序列化和解析循环发送它,它目前正在使用 innerHTML

Array object has toString that can be overwritten, but why DOM elements don't have this

An Array is a native JavaScript object. The ECMAScript standard lays down the requirements of how JS objects work, including the ability to hack a different toString into the prototype.

However the DOM objects are not specified anywhere to be native-JS objects or to adhere to the general rules of behaviour for JS objects. They are permitted to be what the standard calls ‘host objects’, which are much less well-specified.

Hacking their prototypes is very unreliable and not at all a good idea. One JS library, prototype.js, tried, and the results were messy.

Please let Node inherits from Object. Please

Sorry, Stack Overflow has no authority to require Node to be a native JavaScript object!

Until you can persuade all the browser vendors to standardise on native JS objects for the DOM, you will have to use an external function Node_getOuterHTML(node) { ... } and call it explicitly, "click this link:"+Node_getOuterHTML(a).

Although, it would generally be better to append the node you created to the document directly rather than send it through the DOM->HTML-markup->DOM serialise-and-parse cycle it's undergoing at the moment with innerHTML.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文