简体   繁体   English

Raphaeljs 和 Internet Explorer,单击元素时出现问题

[英]Raphaeljs and Internet Explorer, problem when clicking an element

I have the following piece of code in javascript that basically hide or show a Raphaeljs set when I click on it.我在 javascript 中有以下代码,当我单击它时基本上隐藏或显示 Raphaeljs 集。 It works perfectly well under Google Chrome, FireFox and Safari, but not at all under Internet Explorer.它在 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();
};

Anyone has any ideas?有人有什么想法吗? You can test it at http://raphaeljs.com/playground.html by making a cut and paste and omitting the first line of the script.您可以在http://raphaeljs.com/playground.html通过剪切和粘贴并省略脚本的第一行来测试它。 Clicking the "No" should make a box appears, at least in Chrome, but not in IE...单击“否”应该会出现一个框,至少在 Chrome 中,但在 IE 中不...

Thank you!谢谢!

roughly similar to another question and I will post the same answer:与另一个问题大致相似,我将发布相同的答案:

onmousedown worked for me in IE 7,8, and 9 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();
            };

I tried some other methods as well, abstracting the function to a variable but it didnt work.我也尝试了一些其他方法,将 function 抽象为一个变量,但它没有用。 In my case I cannot add a rectangle to the display and have people click on this, it was a poor solution for several reasons.在我的情况下,我无法在显示屏上添加一个矩形并让人们点击它,这是一个糟糕的解决方案,原因有几个。

Hope this helps!希望这可以帮助!

Raphael uses VML to render shapes in IE8, specifically the VML textpath element in your example. Raphael 使用 VML 在 IE8 中呈现形状,特别是您示例中的 VML textpath元素。 However, IE8 does not fire mouse events for that element.但是,IE8 不会触发该元素的鼠标事件。 You could go up the node tree and attach your event handler to the shape element that contains the textpath but, even then, the active-area only consists of the pixels that make up the text, so it's very difficult to click.您可以在节点树上textpath并将您的事件处理程序附加到包含文本路径的shape元素,但即便如此,活动区域仅由构成文本的像素组成,因此很难单击。

A better solution would be to add a transparent rectangle behind the text and attach your event handler to that as well:更好的解决方案是在文本后面添加一个透明矩形并将您的事件处理程序也附加到该矩形:

...

// 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();
  }
);

...

Working Demo: http://jsfiddle.net/brianpeiris/8CZ8G/show/ (Editable via: http://jsfiddle.net/brianpeiris/8CZ8G/ )工作演示: http://jsfiddle.net/brianpeiris/8CZ8G/show/ (可编辑: http://jsfiddle.net/brianpeiris/8CZ8G/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM