removeAttr部分工作

时间:2015-05-29 14:54:15

标签: javascript

我正在尝试在页面加载到内联css时实现removeAttr。它部分起作用,但不适用于所有事情。

这是一个用于演示问题https://jsfiddle.net/argilmour/efmxoL0h/

的JSfiddle
$(document).ready(function(){
   $('.text_default').children().removeAttr('style'); 
});

3 个答案:

答案 0 :(得分:3)

您可以尝试此操作,children.text_default的所有后代不匹配。否则,find("*")将匹配所有孩子及其后代。

$('.text_default').find("*").removeAttr('style'); 

<强> Live a demo

答案 1 :(得分:3)

.children()仅适用于元素的直接子元素。如果您要从所有后代中删除style属性,请说:

$(document).ready(function(){
  $('.text_default').find('*').removeAttr('style'); 
});

或只是:

$(document).ready(function(){
  $('.text_default *').removeAttr('style'); 
});

&#13;
&#13;
$(document).ready(function() {
  $('.text_default *').removeAttr('style');
});
&#13;
.text_default {
  font-size: 14px;
  line-height: 16px;
  font-family: verdana;
}
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<tr class="layout_default">
  <td class="layout_default" align="left" valign="middle">
    <div class="repcontent">
      <div class="text_default">
        <p dir="ltr" style="font-family: Verdana, Arial; font-size: 11px; text-align: justify; line-height: 1; margin-top: 5pt; margin-bottom: 0pt;"><span style="font-size: 13px; font-family: Arial; vertical-align: baseline; white-space: pre-wrap; background-color: transparent;">Special conditions apply to the collection and holding of &quot;sensitive&quot; data. In this case you must obtain the </span>
          <span
          style="font-size: 13px; font-family: Arial; font-style: italic; vertical-align: baseline; white-space: pre-wrap; background-color: transparent;">specific</span><span style="font-size: 13px; font-family: Arial; vertical-align: baseline; white-space: pre-wrap; background-color: transparent;"> consent of the student/learner (ideally in writing). You may encounter this when gathering information before arranging work placements, outdoor activities or when taking photographs you intend to use later, perhaps for publicity or publication purposes. &quot;Sensitive Data&quot; has a special definition under the Data Protection Act and covers:</span>
        </p>
        <ul>
          <li><span style="font-family: Arial; vertical-align: baseline; white-space: pre-wrap; background-color: transparent;">race or ethnic origin </span>
          </li>
          <li><span style="font-family: Arial; vertical-align: baseline; white-space: pre-wrap; background-color: transparent;">political belief religious or other beliefs </span>
          </li>
          <li><span style="font-family: Arial; vertical-align: baseline; white-space: pre-wrap; background-color: transparent;">trade union membership (or otherwise) </span>
          </li>
          <li><span style="font-family: Arial; vertical-align: baseline; white-space: pre-wrap; background-color: transparent;">sexual life </span>
          </li>
          <li><span style="font-family: Arial; vertical-align: baseline; white-space: pre-wrap; background-color: transparent;">physical or mental health or condition (including pregnancy, learning or physical disability) </span>
          </li>
          <li><span style="font-family: Arial; vertical-align: baseline; white-space: pre-wrap; background-color: transparent;">actual or alleged criminal records or activities </span>&nbsp;</li>
        </ul>
      </div>
    </div>
  </td>
</tr>
&#13;
&#13;
&#13;

答案 2 :(得分:1)

我认为您需要更改JQuery选择器。将您的JavaScript更改为:

$('.text_default *').removeAttr('style');

根据你的JSFiddle,你忘了添加JQuery。

在HTML代码中添加:

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.4/jquery.min.js"></script>

在JSFiddle中,在网页左侧的列中添加库。