使用jQuery的unwrap()来删除一个类

时间:2016-06-23 03:54:56

标签: jquery unwrap

我想从表中特定单元格中具有该类的所有标记中删除emphasis类。我正在选择表格单元格内容,如下所示:

var answer = $('tr.problem_display_work:last td:first').html();

在代码中的其他位置,我使用unwrap()这样:

$(".emphasis").contents().unwrap();

但是,我不确定如何将两者结合起来。我尝试了这个,但它失败了:

var answer = $('tr.problem_display_work:last td:first').html().(".emphasis").contents().unwrap();

有什么想法吗?

编辑澄清#1:我想将HTML保留在标记内。所以,如果我有:14 + 6 = <span class="emphasis">20</span>我希望最终得到14 + 6 = 20

编辑澄清#2:有问题的HTML被设置为隐藏<input>字段的值,例如$('#answer').val(answer);。 Gleb Kemarsky的解决方案很好,但由于某种原因它没有应用输入字段的值。

编辑澄清#3 ......

我有<table>一些行和单元格。在最后一行的第一个单元格中,我有一些内容(例如14 + 6 = <span class="emphasis">20</span>)。

我使用var answer = $('tr.problem_display_work:last td:first').html();获取该单元格的HTML,然后使用$('#answer').val(answer);更新隐藏<input id="answer">的值。

当发生这种情况时,我想将14 + 6 = <span class="emphasis">20</span>转换为14 + 6 = 20以获取隐藏<input id="answer">的值,但将其作为14 + 6 = <span class="emphasis">20</span>保留在表格中

编辑澄清#4 ......

关键是要删除任何<span>emphasis

此.. <span class="emphasis"><div class="fraction_display"><span class="numer_display">21</span><span class="bar_display">/</span><span style="border-top-color: green;" class="denom_display">23</span></div></span>

最终应该...... <div class="fraction_display"><span class="numer_display">21</span><span class="bar_display">/</span><span style="border-top-color: green;" class="denom_display">23</span></div>

2 个答案:

答案 0 :(得分:2)

如何从答案中删除<span>标记

5)创建HTML元素的副本并应用.unwrap()方法

同样的问题是solved by Redu

4)从答案中删除<span class="emphasis"></span>(没有嵌套标签)

var answer = $( 'tr.problem_display_work:last td:first' ).html();
$( '#answer' ).val( answer.replace( /<span class="emphasis">(.*?)<\/span>/g, '$1' ) );
.accent,
.emphasis {
  font-family: sans-serif;
  font-weight: bold;
  padding: 0 2px;
}
.accent   { background: yellow;  }
.emphasis { background: orange; }
table {
  margin-bottom: 12px;
}
td {
  border: solid 1px #999;
  padding: 2px 8px;
}
<table>
  <tr class="problem_display_work">
    <td><span class="accent">1</span> + <span class="accent">1</span> = <span class="emphasis">2</span></td>
    <td><span class="accent">2</span> + <span class="accent">2</span> = <span class="emphasis">4</span></td>
  </tr>
  <tr class="problem_display_work">
    <td><span class="accent">3</span> + <span class="accent">3</span> = <span class="emphasis">6</span></td>
    <td><span class="accent">4</span> + <span class="accent">4</span> = <span class="emphasis">8</span></td>
  </tr>
</table>

<input id="answer" size="70">

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

3)通过the .replace() method

从答案中删除所有html标签

var answer = $( 'tr.problem_display_work:last td:first' ).html();
$( '#answer' ).val( answer.replace( /<[^>]+>/g, '' ) );
.emphasis {
  background: orange;
  font-family: sans-serif;
  font-weight: bold;
  padding: 0 2px;
}
table {
  margin-bottom: 12px;
}
td {
  border: solid 1px #999;
  padding: 2px 8px;
}
<table>
  <tr class="problem_display_work">
    <td>1 + 1 = <span class="emphasis">2</span></td>
    <td>2 + 2 = <span class="emphasis">4</span></td>
  </tr>
  <tr class="problem_display_work">
    <td>3 + 3 = <span class="emphasis">6</span></td>
    <td>4 + 4 = <span class="emphasis">8</span></td>
  </tr>
</table>

<input id="answer">

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

2)将.emphasis类添加到选择器

$( 'tr.problem_display_work:last td:first .emphasis' ).contents().unwrap();
.emphasis {
  background: orange;
  font-family: sans-serif;
  font-weight: bold;
  padding: 0 2px;
}
td {
  border: solid 1px #999;
  padding: 2px 8px;
}
<table>
  <tr class="problem_display_work">
    <td>1 + 1 = <span class="emphasis">2</span></td>
    <td>2 + 2 = <span class="emphasis">4</span></td>
  </tr>
  <tr class="problem_display_work">
    <td>3 + 3 = <span class="emphasis">6</span></td>
    <td>4 + 4 = <span class="emphasis">8</span></td>
  </tr>
</table>

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

1)使用the .find() method

selectorAnswer = $( 'tr.problem_display_work:last td:first' );
selectorAnswer.find( '.emphasis' ).contents().unwrap();

答案 1 :(得分:0)

你的问题不明确。我知道你想从表格的所有单元格中删除“强调”类。解决方案是......

$('tr.problem_display_work:last td:first')。removeClass(“emphasis”);