0

JavaScriptでオブジェクトを識別しようとしています。私の問題は、msie DOM ノードがオブジェクトの子孫のインスタンスではないため、そのようなインスタンスのプロパティを設定または取得できないことです。私は htc の動作を使用してその回避策を作成しようとしています (スタイルを持つノードのみが動作を持つことができるため、これは半分の解決策ですが、何もないよりはましです):

ID.htc:

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

test.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 に答える