Does the attr() in jQuery force lowercase?

后端 未结 4 754
广开言路
广开言路 2020-12-03 20:29

I\'m trying to manipulate the svg \'viewBox\' attribute which looks something like this:

 ...          


        
4条回答
  •  情歌与酒
    2020-12-03 21:13

    Per http://www.w3.org/TR/xhtml1/#h-4.2 "XHTML documents must use lower case for all HTML element and attribute names."

    So in order to avoid having the attribute convert to lowercase in an XHTML document you need to create the element specifying a namespace using document.createElementNS(), like:

    var svg = document.createElementNS('http://www.w3.org/2000/svg', 'svg');
        svg.setAttribute('viewBox', '0 0 512 512’);
    

    If you plan to add a element you also need to specify the namespace while creating the element as well as the xlink:href attribute, like:

    var use = document.createElementNS('http://www.w3.org/2000/svg','use');
        use.setAttributeNS('http://www.w3.org/1999/xlink', 'xlink:href', '#your_svg_id');
    

提交回复
热议问题