Raphaeljs and Internet Explorer, problem when clicking an element

后端 未结 2 662
南方客
南方客 2021-01-15 06:36

I have the following piece of code in javascript that basically hide or show a Raphaeljs set when I click on it. It works perfectly well under Google Chrome, FireFox and Saf

相关标签:
2条回答
  • 2021-01-15 06:44

    roughly similar to another question and I will post the same answer:

    onmousedown worked for me in IE 7,8, and 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. 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!

    0 讨论(0)
  • 2021-01-15 07:06

    Raphael uses VML to render shapes in IE8, specifically the VML textpath element in your example. However, IE8 does not fire mouse events for that element. 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.

    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/)

    0 讨论(0)
提交回复
热议问题