最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Read Attributes of SVG-Elements in HTML via JS - Stack Overflow

programmeradmin0浏览0评论

I have the following markup (HTML with native SVG):

<!doctype html>
   <!-- ...    
        html-Elements 
        ... --> 
   <svg version="1.1" ... >
        <defs> <circle id="infop" cx="0" cy="0" r="9" /> </defs>
        <!-- ... 
             svg Elements
             ... --> 
        <svg> <!-- to have separate coordinate-system -->
            <g id="outSvg"></g>
        </svg>
    ...

A js-method outputs a line and several <use href="infotop"> Elements to #outSvg (to become a graph). The <use> Elements have onmouseover-Events to show tooltips.

Now, when it comes to retrieving the coordinates in the onmouseover-function of the <use> i run into problems:

I tried the following different approaches to retrieve the values:

function showInfo(evt){

    console.log("target: "+evt.target);
    console.log("AttrNS: "+evt.target.getAttributeNS(null,"x"));
    console.log("Attrib: "+evt.target.getAttribute("x"));
    console.log("basVal: "+evt.target.x.baseVal.value);
    console.log("corrEl: "+evt.target.correspondingUseElement.x.baseVal.value);

producing the following output:

    //target -> ** [object SVGUseElement] in FF, in all other browsers: [object SVGElementInstance])
    //AttrNS -> Works only in FF
       // * Uncaught TypeError: Object #<SVGElementInstance> has no method 'getAttributeNS'
    //Attrib -> Works only in FF
       // * Uncaught TypeError: Object #<SVGElementInstance> has no method 'getAttribute'
    //basVal -> works only in FF
       // * Uncaught TypeError: Cannot read property 'baseVal' of undefined
    //corrEl -> fails in FF but works in Ch, O and IE

Browsers:FF10, Ch16, O11.61, IE9

Question:

Why is getAttribute() failing in the other browsers? Am I missing something important? Is there a consistent way to retrieve the values crossbrowser? (Besides via a switch between evt.target.x and evt.target.correspondingUseElement.x)

important: vanilla js only, and I know about browserswitches, thats not the point! I'll provide a fiddle if needed, as soon as i find the time. Finally - thank you for reading this!

EDIT: * added the js-errors

EDIT2: ** FF returns another Object than the other browsers

I have the following markup (HTML with native SVG):

<!doctype html>
   <!-- ...    
        html-Elements 
        ... --> 
   <svg version="1.1" ... >
        <defs> <circle id="infop" cx="0" cy="0" r="9" /> </defs>
        <!-- ... 
             svg Elements
             ... --> 
        <svg> <!-- to have separate coordinate-system -->
            <g id="outSvg"></g>
        </svg>
    ...

A js-method outputs a line and several <use href="infotop"> Elements to #outSvg (to become a graph). The <use> Elements have onmouseover-Events to show tooltips.

Now, when it comes to retrieving the coordinates in the onmouseover-function of the <use> i run into problems:

I tried the following different approaches to retrieve the values:

function showInfo(evt){

    console.log("target: "+evt.target);
    console.log("AttrNS: "+evt.target.getAttributeNS(null,"x"));
    console.log("Attrib: "+evt.target.getAttribute("x"));
    console.log("basVal: "+evt.target.x.baseVal.value);
    console.log("corrEl: "+evt.target.correspondingUseElement.x.baseVal.value);

producing the following output:

    //target -> ** [object SVGUseElement] in FF, in all other browsers: [object SVGElementInstance])
    //AttrNS -> Works only in FF
       // * Uncaught TypeError: Object #<SVGElementInstance> has no method 'getAttributeNS'
    //Attrib -> Works only in FF
       // * Uncaught TypeError: Object #<SVGElementInstance> has no method 'getAttribute'
    //basVal -> works only in FF
       // * Uncaught TypeError: Cannot read property 'baseVal' of undefined
    //corrEl -> fails in FF but works in Ch, O and IE

Browsers:FF10, Ch16, O11.61, IE9

Question:

Why is getAttribute() failing in the other browsers? Am I missing something important? Is there a consistent way to retrieve the values crossbrowser? (Besides via a switch between evt.target.x and evt.target.correspondingUseElement.x)

important: vanilla js only, and I know about browserswitches, thats not the point! I'll provide a fiddle if needed, as soon as i find the time. Finally - thank you for reading this!

EDIT: * added the js-errors

EDIT2: ** FF returns another Object than the other browsers

Share Improve this question edited Feb 15, 2012 at 10:23 Christoph asked Feb 14, 2012 at 10:59 ChristophChristoph 51.2k21 gold badges101 silver badges128 bronze badges 4
  • Seems you already found a way. Use either target.x or target.correspondingUseElement.x. Should not be difficult to create a wrapper function for SVG attribute access. – Felix Kling Commented Feb 14, 2012 at 11:06
  • Well, I find your solution rather unsatisfying... – Christoph Commented Feb 14, 2012 at 11:26
  • @Christoph, people spend their own time to try to help you FOR FREE. Shouldn't you be grateful? – Allen Zhang Commented Nov 25, 2014 at 0:53
  • @Allen I explicitely asked if there is a way that works crossbrowser without using a wrapper function - am I not allowed to express my feelings about an unsatisfactory answer then (which suggests a solution I already figured out by myself anyway)? Erik for example provided some very useful information, that's why I upvoted his answer and explicitely thanked him. – Christoph Commented Nov 25, 2014 at 21:28
Add a comment  | 

4 Answers 4

Reset to default 9

Well, after reading Erik Dahlström's answer, I noticed that FF behaves incorrectly. It should return an Element-Instance instead of the Use-Element directly.

I use the following code now:

var el = (evt.target.correspondingUseElement)?evt.target.correspondingUseElement:evt.target;

console.log(el.getAttribute("x"));

This way I can retrieve the attributes via getAttribute() consistently in all browsers.

can you try this? evt.target.getAttributeNode("x").nodeValue . I tried this in safari,chrome,Firefox its working fine.

As far as I know Firefox doesn't support SVGElementInstance.

Here are a couple of tests for SVGElementInstance from the w3c SVG 1.1 Second Edition testsuite to verify:

  • struct-dom-12-b
  • struct-dom-14-f
  • struct-dom-15-f

What you should do is to provide a fallback solution if the SVGElementInstance isn't there, which is easy enough to detect, e.g:

if (elm.correspondingUseElement) {
  /* elm is a used instance, not a real element */
} else {
  /* fallback solution */
}

If the element is an SVGElementInstance it will have the correspondingUseElement property, otherwise it won't have it. Normal svg elements will not have this property, only used instances will have it.

Did you try evt.target.getAttributeNS(evt.target.parent.namespaceURI,"x") ?

发布评论

评论列表(0)

  1. 暂无评论