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

javascript - Keep uppercase using attr() with jquery (case sensitive) - Stack Overflow

programmeradmin3浏览0评论

I am doing this with jQuery :

@xmlOut = $('<rules />')
@xmlOut.attr('xsi:schemaLocation','test')

I get this :

<rules xsi:schemalocation='test'></rules>

The "L" is not uppercase anymore...

I am doing this with jQuery :

@xmlOut = $('<rules />')
@xmlOut.attr('xsi:schemaLocation','test')

I get this :

<rules xsi:schemalocation='test'></rules>

The "L" is not uppercase anymore...

Share Improve this question edited Nov 20, 2012 at 10:19 Sebastien asked Nov 20, 2012 at 10:10 SebastienSebastien 6,66014 gold badges59 silver badges105 bronze badges
Add a comment  | 

3 Answers 3

Reset to default 10

There is a ticket http://bugs.jquery.com/ticket/11166

Alternatively, you can add attribute hook (with lowercase name) to jQuery in order to use desired setter method. For example:

$.attrHooks['viewbox'] = {
    set: function(elem, value, name) {
        elem.setAttributeNS(null, 'viewBox', value + '');
        return value;
    }
};

Then you can set the attribute case sensitive with .attr():

$('svg').attr('viewBox', '0 0 100 100');

Try using plain Javascript's setAttribute which is not case sensitive.

@xmlOut.get(0).setAttribute('xsi:schemLocation', 'test');

Kevin's answer is incorrect, .setAttribute() will change the attribute name to lowercase.

Instead, use element.setAttributeNS() with an empty string for the first parameter.

@xmlOut.get(0).setAttributeNS('', 'xsi:schemaLocation','test')

https://developer.mozilla.org/en-US/docs/Web/API/Element/setAttributeNS

发布评论

评论列表(0)

  1. 暂无评论