jquery删除样式属性

时间:2018-03-27 14:49:59

标签: javascript jquery

我想从HTML元素中删除style属性,但只从一个div中删除,而不是完整的html代码。例如:

<div class="row" style="width: 100px"></div>
<div class="container"><p style="font-size: 12px"></p> </div>

我只想删除div中所有元素的'style',类为'container'。谢谢你的帮助!

4 个答案:

答案 0 :(得分:1)

您可以使用removeAttr()&amp; children()喜欢:

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

样本:

&#13;
&#13;
$('div.container')
  .children()   // Get the children of each element in the set of matched elements
  .removeAttr('style'); // Remove style attribute from each element
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="row" style="width: 100px"></div>
<div class="container">
  <p style="font-size: 12px">Test 1</p>
  <p style="color: green; font-size: 12px">Test 2</p>
</div>
<hr/>
<div class="container2">
  <p style="font-size: 12px">Test 1</p>
  <p style="color: green; font-size: 12px">Test 2</p>
</div>
&#13;
&#13;
&#13;

答案 1 :(得分:1)

jQuery('.container*').attr('style','');

答案 2 :(得分:0)

你可以试试这个

$(".container").removeAttr("style")

答案 3 :(得分:0)

您可以使用:$('.container *').attr('style', '');

&#13;
&#13;
$('.container *').attr('style', '');
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="row" style="width: 100px"></div>
<div class="container"><p style="font-size: 12px">Test</p> </div>
&#13;
&#13;
&#13;

OR:$('.container *').removeAttr('style');

&#13;
&#13;
$('.container *').removeAttr('style');
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="row" style="width: 100px"></div>
<div class="container"><p style="font-size: 12px">Test</p> </div>
&#13;
&#13;
&#13;