Javascript 移除 HTML 标签的属性
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3909555/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Remove attribute of HTML tag
提问by DGT
Is it possible to remove the attribute of the first HTML <div>tag? So, this:
是否可以删除第一个 HTML<div>标签的属性?所以这:
<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>?
采纳答案by Nick Craver
To remvove it from literally the first element use .removeAttr():
要将其从字面上的第一个元素中删除,请使用.removeAttr():
$(":first").removeAttr("style");
or in this case .show()will show the element by removing the displayproperty:
或者在这种情况下.show()将通过删除display属性来显示元素:
$(":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 :hiddenas your selector:
如果要显示第一个隐藏的,请:hidden用作选择器:
$(":hidden:first").show();
回答by Phoenix
Or pure JavaScript:
或者纯 JavaScript:
document.getElementById('id?').removeAttribute('attribute?')
回答by Alex
Yes, in fact jQuery has something for this purpose: http://api.jquery.com/removeAttr/
是的,事实上 jQuery 有一些用于此目的的东西:http: //api.jquery.com/removeAttr/
回答by Sarfraz
You can use the removeAttrmethod like this:
你可以使用这样的removeAttr方法:
$('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.
由于您没有为 div 指定任何 id 或 class,上面的代码找到一个具有内联样式的 div,然后从中删除该样式。
If you know there is some parent element of the div with an id, you can use this code instead:
如果您知道 div 的某个父元素带有 id,则可以改用以下代码:
$('#parent_id div[style]').removeAttr('style');
Where parent_idis supposed to be the id of parent element containing the div under question.
哪里parent_id应该是包含有问题的 div 的父元素的 id。
回答by VoteyDisciple
You say "remove the attribute" —?do you mean to remove allattributes? Or remove the styleattribute specifically?
你说“删除属性”——你的意思是删除所有属性吗?还是style专门删除该属性?
Let's start with the latter:
让我们从后者开始:
$('div').removeAttr('style');
The removeAttrfunction simply removes the attribute entirely.
该removeAttr函数只是完全删除该属性。
回答by Anoop
it is easy in jQuery just use
在jQuery中很容易使用
$("div:first").removeAttr("style");
in javascript
在 JavaScript 中
use var divs = document.getElementsByTagName("div");
使用 var divs = document.getElementsByTagName("div");
divs[0].removeAttribute("style");
divs[0].removeAttribute("style");

