admin管理员组文章数量:1134232
I was wondering if this is possible.
There's this element
<div id="sample_id" style="width:100px; height:100px; color:red;">
So I want to remove width:100px; and height:100px;
the result would be
<div id="sample_id" style="color:red;">
Any help would be apprreciated. :)
I was wondering if this is possible.
There's this element
<div id="sample_id" style="width:100px; height:100px; color:red;">
So I want to remove width:100px; and height:100px;
the result would be
<div id="sample_id" style="color:red;">
Any help would be apprreciated. :)
Share Improve this question edited Sep 9, 2013 at 4:56 Praveen Prasannan 7,12310 gold badges52 silver badges71 bronze badges asked Sep 9, 2013 at 4:51 Brian CoolidgeBrian Coolidge 4,6499 gold badges52 silver badges82 bronze badges 2- 2 Check the question tags.... he is asking how he can remove a CSS rule from an element with JavaScript... – Thanos Commented Sep 9, 2013 at 5:15
- 2 Possible duplicate of removing html element styles via javascript – T.Todua Commented Aug 31, 2016 at 12:34
8 Answers
Reset to default 232You can edit style with pure Javascript. No library needed, supported by all browsers
var element = document.getElementById('sample_id');
element.style.width = '';
element.style.height = '';
For more information, you can refer to HTMLElement.style documentation on MDN.
var element = document.getElementById('sample_id');
element.style.removeProperty("width");
element.style.removeProperty("height");
Use javascript
But it depends on what you are trying to do. If you just want to change the height and width, I suggest this:
{
document.getElementById('sample_id').style.height = '150px';
document.getElementById('sample_id').style.width = '150px';
}
TO totally remove it, remove the style, and then re-set the color:
getElementById('sample_id').removeAttribute("style");
document.getElementById('sample_id').style.color = 'red';
Of course, no the only question that remains is on which event you want this to happen.
You can simply set that style to [unset], It forces CSS to pretend that style was never assigned to the given element.
var element = document.getElementById('sample_id');
element.style.width = "unset";
element.style.height = "unset";
Update: For a better approach, please refer to Blackus's answer in the same thread.
If you are not averse to using JavaScript and Regex, you can use the below solution to find all width
and height
properties in the style
attribute and replace
them with nothing.
//Get the value of style attribute based on element's Id
var originalStyle = document.getElementById('sample_id').getAttribute('style');
var regex = new RegExp(/(width:|height:).+?(;[\s]?|$)/g);
//Replace matches with null
var modStyle = originalStyle.replace(regex, "");
//Set the modified style value to element using it's Id
document.getElementById('sample_id').setAttribute('style', modStyle);
$("#sample_id").css({ 'width' : '', 'height' : '' });
Specifying auto on width and height elements is the same as removing them, technically. Using vanilla Javascript:
images[i].style.height = "auto";
images[i].style.width = "auto";
Just use like this
$("#sample_id").css("width", "");
$("#sample_id").css("height", "");
本文标签: javascriptRemove Style on ElementStack Overflow
版权声明:本文标题:javascript - Remove Style on Element - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736762742a1951642.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论