简体   繁体   中英

Remove attribute of HTML tag

Is it possible to remove the attribute of the first HTML <div> tag? So, this:

<div style="display: none; ">aaa</div>

becomes

<div>aaa</div>

from the following:

<div style="display: none; ">aaa</div>
<a href="#" style="display: none; ">(bbb)</a>
<span style="display: none; ">ccc</span>​

或纯JavaScript:

document.getElementById('id?').removeAttribute('attribute?')

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();

是的,实际上jQuery有这个目的: http//api.jquery.com/removeAttr/

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.

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.

it is easy in jQuery just use

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

in javascript

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM