无法删除Span元素的类

时间:2017-11-20 00:26:54

标签: javascript jquery html html5

我正在尝试删除"bg-color-disable"的课程#filenameRPT,但我没有尝试过。我使用过$("#parentRPT > span#filenameRPT").removeClass("bg-color-disable");$("#filenameRPT").removeClass("bg-color-disable");但没有运气。有什么想法吗?

<div class="two-row mb-3">
  <div id="parentRPT" class="input-group bg-color-disable">
    <span class="input-group-addon gi data-gi-size gi-file-plus"></span>
    <span id="filenameRPT" class="bg-color-disable">
      Upload REPORT
    <span class="fa fa-arrow-circle-right text-primary ml-1 mr-1"></span>
      <a name="rpt" href="#">No File Exists</a
      <input class="hidden" name="delete_RPT" value="not_deleted" type="text"/>
    </span>
    <span id="deleteRPT" class="fa fa-times text-danger hidden" title="Delete"></span>
    <label class="input-group-addon has-float-label" for="file-uploadRPT">
      Browse
      <input id="file-uploadRPT" name="FileToUploadRPT" type="file" accept="application/pdf">
    </label>
  </div>
</div>

疯狂的是我能够使用#parentRPT删除$("#parentRPT").removeClass("bg-color-disable");的“bg-color-disable”,但使用$("#filenameRPT").removeClass("bg-color-disable");对元素#filenameRPT不起作用。为什么呢?

1 个答案:

答案 0 :(得分:0)

我认为你的问题是锚标签的关闭不当。 删除课程很简单。

window.onload = removeClass()

function removeClass() {
   console.log('before removal',document.getElementById('filenameRPT').classList);
   document.getElementById('filenameRPT').classList.remove('bg-color-disable')
   console.log('after removal',document.getElementById('filenameRPT').classList);
}
<div class="two-row mb-3">
  <div id="parentRPT" class="input-group bg-color-disable">
    <span class="input-group-addon gi data-gi-size gi-file-plus"></span>
    <span id="filenameRPT" class="bg-color-disable">
      Upload REPORT
    <span class="fa fa-arrow-circle-right text-primary ml-1 mr-1"></span>
      <a name="rpt" href="#">No File Exists</a>
      <input class="hidden" name="delete_RPT" value="not_deleted" type="text"/>
    </span>
    <span id="deleteRPT" class="fa fa-times text-danger hidden" title="Delete"></span>
    <label class="input-group-addon has-float-label" for="file-uploadRPT">
      Browse
      <input id="file-uploadRPT" name="FileToUploadRPT" type="file" accept="application/pdf">
    </label>
  </div>
</div>