I need to enumerate all the properties of my DOM elements which I previously modified. I could do it with Firefox, Chrome and Opera but I could not with Safari (I don't care about IE for the moment).
<a id="link">Link...</a>
<script>
var link = document.getElementById("link");
var foo = function (baz) {};
link.onclick = foo;
alert ("onclick" in link); // true
alert (link.hasOwnProperty("onclick")); // true
alert (link.propertyIsEnumerable("onclick"));
// false with Chrome, Safari, Opera*
</script>
*: Although the property is not enumerable in Opera it is anyway enumerated!!!
for (p in link)
if (p==="onclick")
alert (p); // onclick
I can make the third alert
output true
in Chrome by deleting the onclick
property before assigning it:
delete link.onclick;
But the property is not yet enumerated in Safari.
I even tried with the EcmaScript 5 Object method de开发者_Python百科fineProperty:
Object.defineProperty (link, onclick, {
value: foo,
enumerable: true,
configurable: true,
writable: true
});
but it returns the error:
TypeError: defineProperty is not supported on DOM Objects
Any suggestions?
P.S. Why does Safari behave different from Chrome although they are both based on Webkit?
Compare the useragent to the Webkit version in which the EcmaScript5 Object methods were implemented in Webkit JavaScriptCore. The Webkit version of Chrome is different from that of Safari, so standards support will vary accordingly.
精彩评论