0

我正在尝试识别 javascript 中的对象。我的问题是,msie DOM 节点不是任何 Object 后代的实例,因此我无法在此类实例上设置或获取属性。我正在尝试使用 htc 行为解决此问题(只有具有样式的节点才能具有行为,所以这是一个半解决方案,但总比没有好):

身份.htc:

<PUBLIC:COMPONENT>
<script type="text/javascript">
for(property in Object.prototype)
{
    element[property]=Object.prototype[property];
}
</script>
</PUBLIC:COMPONENT>

测试.html:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
<title>javascript identity workaround</title>

<!--[if IE]>
<style type="text/css">
*{behavior:url(identity.htc)}
</style>
<![endif]-->

<script type="text/javascript">

(function ()
{
    var i=0;
    var table={};
    Object.prototype.identity=function ()
    {
        if (!this.__hashcode__)
        {
            this.__hashcode__=++i;
            table[i]=this;
        }
        return this.__hashcode__;
    };
    Object.identify=function (i)
    {
        return table[i];
    };
})();

window.onload=function ()
{
    var body=document.getElementsByTagName("body")[0];
    var existingElement=document.getElementById("existingElement");
    var newElement=document.createElement("div");
        newElement.addBehavior("identity.htc");
    alert(body.identity()); //1
    alert(existingElement.identity()); //2
    alert(newElement.identity()); // expected: 3, real: method not supported
};

</script>
</head>
<body>
    <div id="existingElement"></div>
</body>
</html> 

我的问题是,我不能在新创建的元素上使用标识方法。我尝试使用 addBehavior 方法添加行为,但没有帮助。有人对此有解决方案吗?

4

1 回答 1

0

我得到了解决方案:

<PUBLIC:COMPONENT lightWeight="true">
于 2011-04-06T20:35:33.957 回答