【问题标题】:Raphaeljs and Internet Explorer, problem when clicking an elementRaphaeljs 和 Internet Explorer,单击元素时出现问题
【发布时间】:2011-05-03 18:09:19
【问题描述】:

我在 javascript 中有以下一段代码,当我单击它时基本上隐藏或显示 Raphaeljs 集。它在 Google Chrome、FireFox 和 Safari 下运行良好,但在 Internet Explorer 下完全不行。

var paper = Raphael(document.getElementById('ADiv'), 450, 490);

var group = paper.set();

var toxicRect = paper.rect(0, 0, 120, 60, 10 );
toxicRect.attr({"stroke-width": 1,
            "stroke" : "#3083BE",
            "fill" : "#D1DFE9"});

group.push( toxicRect );

var toxicRectText = paper.text(60, 25, "Toxic in air\nthrough inhalation");
toxicRectText.attr({"font-size": 12 });
group.push( toxicRectText );

var toxicToConcentrationPath = paper.path("M60 60L60 80")
toxicToConcentrationPath.attr({"stroke-width": 1,
                   "stroke" : "#3083BE"});

group.push( toxicToConcentrationPath );

var concentrationRect = paper.rect(0, 80, 120, 60, 10 );
concentrationRect.attr({"stroke-width": 1,
                    "stroke" : "#3083BE",
                "fill" : "#D1DFE9"});

group.push(concentrationRect);

var conRectText = paper.text( 60, 105, "Is concentration\n> TLV/TWA");
conRectText.attr({"font-size": 12});
group.push(conRectText);

var conRectTextYes = paper.text(50, 135, "Yes  / ");

conRectTextYes.attr({"font-size": 12,
                 "font-style": "italic"});

group.push(conRectTextYes);

var conRectTextNo = paper.text(75, 135, "No");
conRectTextNo.attr({"font-size": 12,
                "font-style": "italic"});

group.push(conRectTextNo); 

var monitorConcentrationGroup = paper.set();

var monitorConcentrationRect = paper.rect(140, 95, 60, 30, 10 );
monitorConcentrationRect.attr({"stroke-width": 1,
                        "stroke" : "#3083BE",
                    "fill" : "#D1DFE9"});

monitorConcentrationGroup.push(monitorConcentrationRect);

var monitorConcentrationRectText =  paper.text(170, 115, "Monitor");
monitorConcentrationRectText.attr({"font-size": 12});

monitorConcentrationGroup.push(monitorConcentrationRectText);

var concentrationToMonitorPath = paper.path("M120 110L140 110")
concentrationToMonitorPath.attr({"stroke-width": 1,
                     "stroke" : "#3083BE"});

monitorConcentrationGroup.push(concentrationToMonitorPath);
monitorConcentrationGroup.hide();


//Actions when clicking on decisions
conRectTextYes.node.onclick = function () {

        monitorConcentrationGroup.hide();
};

conRectTextNo.node.onclick = function () {

        monitorConcentrationGroup.show();
};

有人有什么想法吗?您可以在http://raphaeljs.com/playground.html 进行测试,方法是进行剪切和粘贴并省略脚本的第一行。单击“否”应该会出现一个框,至少在 Chrome 中,但在 IE 中不会...

谢谢!

【问题讨论】:

    标签: javascript internet-explorer internet-explorer-8 raphael vml


    【解决方案1】:

    Raphael 使用 VML 在 IE8 中呈现形状,特别是您示例中的 VML textpath 元素。但是,IE8 不会触发该元素的鼠标事件。您可以上节点树并将您的事件处理程序附加到包含 textpath 的 shape 元素,但即便如此,活动区域仅由构成文本的像素组成,因此很难单击.

    更好的解决方案是在文本后面添加一个透明矩形并将您的事件处理程序也附加到该矩形:

    ...
    
    // Make the rectangle slightly larger and offset it 
    // from the text coordinates so that it covers the text.
    var conRectTextNoContainer = paper.rect(75 - 8, 135 - 9, 17, 14);
    // Give the rectangle a fill (any color will do) and 
    // set its opacity to and stroke-width to make it invisible
    conRectTextNoContainer.attr({
      fill: '#000000',
      'fill-opacity': 0,
      'stroke-width': 0
    });
    group.push(conRectTextNoContainer); 
    
    var conRectTextNo = paper.text(75, 135, "No");
    conRectTextNo.attr({
      "font-size": 12,
      "font-style": "italic"
    });
    group.push(conRectTextNo); 
    
    ...
    
    var conRectTextNoNode = conRectTextNo.node;
    if (conRectTextNoNode.tagName === 'textpath') {
      // We're in IE8, attach the handler to the parentNode
      conRectTextNoNode = conRectTextNo.node.parentNode;
    }
    conRectTextNoContainer.node.onclick = (
      conRectTextNoNode.onclick = function () {
        monitorConcentrationGroup.show();
      }
    );
    
    ...
    

    工作演示:http://jsfiddle.net/brianpeiris/8CZ8G/show/(可通过:http://jsfiddle.net/brianpeiris/8CZ8G/ 编辑)

    【讨论】:

      【解决方案2】:

      与另一个问题大致相似,我将发布相同的答案:

      onmousedown 在 IE 7,8 和 9 中为我工作

                  st[0].onclick = function () {
                      myFunc(dataObj);
                      st.toFront();
                      R.safari();
                  };
                  st[0].onmousedown = function () {
                      myFunc(dataObj);
                      st.toFront();
                      R.safari();
                  };
      

      我也尝试了一些其他方法,将函数抽象为变量,但没有奏效。在我的情况下,我无法在显示屏上添加一个矩形并让人们点击它,这是一个糟糕的解决方案,原因有很多。

      希望这会有所帮助!

      【讨论】:

      • 这实际上对我有用。只有 IE8 有这个问题。 IE9 可以很好地处理点击事件,而 IE8 并非总是如此。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-06-12
      • 1970-01-01
      • 2015-02-06
      • 2011-05-04
      • 1970-01-01
      相关资源
      最近更新 更多