Remove attribute of HTML tag

后端 未结 6 2337
无人及你
无人及你 2020-12-01 18:45

Is it possible to remove the attribute of the first HTML

tag? So, this:

aaa
相关标签:
6条回答
  • 2020-12-01 18:52

    To remvove it from literally the first element use .removeAttr():

    $(":first").removeAttr("style");
    

    or in this case .show() will show the element by removing the display property:

    $(":first").show();
    

    Though you probably want to narrow it down to inside something else, for example:

    $("#container :first").removeAttr("style");
    

    If you want to show the first hidden one, use :hidden as your selector:

    $(":hidden:first").show();
    
    0 讨论(0)
  • 2020-12-01 18:55

    Yes, in fact jQuery has something for this purpose: http://api.jquery.com/removeAttr/

    0 讨论(0)
  • 2020-12-01 19:03

    Or pure JavaScript:

    document.getElementById('id?').removeAttribute('attribute?')
    
    0 讨论(0)
  • 2020-12-01 19:11

    You can use the removeAttr method like this:

    $('div[style]').removeAttr('style');
    

    Since you have not specified any id or class for the div, the above code finds a div having inline style in it and then it removes that style from it.

    If you know there is some parent element of the div with an id, you can use this code instead:

    $('#parent_id div[style]').removeAttr('style');
    

    Where parent_id is supposed to be the id of parent element containing the div under question.

    0 讨论(0)
  • 2020-12-01 19:12

    it is easy in jQuery just use

    $("div:first").removeAttr("style");
    

    in javascript

    use var divs = document.getElementsByTagName("div");

    divs[0].removeAttribute("style");

    0 讨论(0)
  • 2020-12-01 19:15

    You say "remove the attribute" — do you mean to remove all attributes? Or remove the style attribute specifically?

    Let's start with the latter:

    $('div').removeAttr('style');
    

    The removeAttr function simply removes the attribute entirely.

    0 讨论(0)
提交回复
热议问题