Does the attr() in jQuery force lowercase?

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

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

 ...          


        
4条回答
  •  天涯浪人
    2020-12-03 21:28

    You could use jQuery hooks:

    ['preserveAspectRatio', 'viewBox'].forEach(function(k) {
      $.attrHooks[k.toLowerCase()] = {
        set: function(el, value) {
          el.setAttribute(k, value);
          return true;
        },
        get: function(el) {
          return el.getAttribute(k);
        },
      };
    });
    

    Now jQuery will will use your setter/getters to manipulate those attributes.

    Note that el.attr('viewBox', null) would have failed; your hook setter won't be called. Instead you should use el.removeAttr('viewBox').

提交回复
热议问题