1

優れたJavaScriptライブラリRaphaelで使用するためにニュージーランドのSVGマップを作成しましたが、残念ながら、IEのJavaScriptインタープリターのバグまたは構文上のバリエーションであるとしか想像できないことに気づきました。

Firefoxやその他のブラウザでは、onlickイベントとonmouseoverイベントは完全に機能しますが、IEでは発生しません(IE 7でテスト済み)。残念ながら、これをデバッグするのに役立つjavascriptエラーはないため、IEがこれらのイベントを根本的に異なる方法で処理すると想定することしかできません。

<script type="text/javascript" charset="utf-8">
    window.onload = function() {
        var R = Raphael("paper", 450, 600);
        var attr = {
            fill: "#3f3f40",
            stroke: "#666",
            "stroke-width": 1,
            "stroke-linejoin": "round"
        };
        var nz = {};
        nz.northland = R.path(attr, "M 193.34222,3.7847503 C 194.65463");
                // SVG data stripped for sake of brevity
        var current = null;
        for (var region in nz) {
            nz[region].color = Raphael.getColor();
            (function(rg, region) {
                rg[0].style.cursor = "pointer";
                rg[0].onmouseover = function() {
                    current && nz[current].animate({ fill: "#3f3f40", stroke: "#666" }, 500) && (document.getElementById(current).style.display = "");
                    rg.animate({ fill: rg.color, stroke: "#ccc" }, 500);
                    rg.toFront();
                    R.safari();
                    document.getElementById(region).style.display = "block";
                    current = region;
                };
                rg[0].onclick = function() {
                    alert("IE never gets this far.");
                    //window.location.href = "my-page.aspx?District=" + region;
                };
                rg[0].onmouseout = function() {
                    rg.animate({ fill: "#3f3f40", stroke: "#666" }, 500);
                };
                if (region == "northland") {
                    rg[0].onmouseover();
                }
            })(nz[region], region);
        }
    };
</script>

どうもありがとう :)

4

4 に答える 4

3

修正は、onclickの代わりにonmousedownイベントを使用しているようです。

変化:

rg[0].onclick = function() {
alert("IE never gets this far, but Firefox is happy.");
};

rg[0].onmousedown = function() {
alert("This works in IE and Firefox.");
};

問題を解決しました。みんなの意見に感謝します-結局そこに着きました。IEがonclickを嫌う理由を誰かが実際に知っているなら、私は聞いてみたいです!

于 2009-03-29T20:33:17.037 に答える
1

イベントを添付してみましたか?

if (rg[0].attachEvent)
    rg[0].attachEvent("onclick", function(){ /* IE */ });
else
    rg[0].addEventListener("click", function(){ /* other */ }, false);
于 2009-03-29T20:24:31.753 に答える
0

一般に、jqueryやプロトタイプなどの抽象化フレームワークが最善の策です。彼らはあなたのためにブラウザの違いを処理します。また、より高いレベルでイベントをサブスクライブできます...ブラウザでは、mousemove / clickをサブスクライブして、何が終わったかを判断したり、イベントバブルからクリックしたりする方が多くのオブジェクトよりも安価です。

Joel Potterは、domメソッドとIEメソッドを使用したサブスクライバーモデルについて言及しました。これは、メソッドの割り当てよりも優れた方法です。

于 2009-03-30T06:00:07.907 に答える
0

IE が正しく動作することは正確にはわかっていません。使用している IE のバージョンを教えていただけると助かります。

于 2009-03-29T20:25:08.587 に答える