Is using custom attributes valid?

前端 未结 3 1553
感情败类
感情败类 2021-01-14 00:22

I want to cancel any links and add extra attributes to each one. Below is how I\'ve achieved this.

function anularEnlaces(){
    $(\'nav a\').each(function()         


        
3条回答
  •  青春惊慌失措
    2021-01-14 00:59

    No, you should use data() instead for custom element attributes, and attr() ( or better yet prop() ) for standard html attributes .


    Example of data() usage :

    test
    
    // getter
    var customValue = $('a').data('custom');
    var customUndefined = $('a').data('testing');
    

    Now customValue contains "value" string, and customUndefined contains undefined

    // setter
    $('a').data('custom', 'test');
    $('a').data('testing', true);
    

提交回复
热议问题