0

我想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);用来更新 hidden 的值<input id="answer">

发生这种情况时,我想14 + 6 = <span class="emphasis">20</span>转换14 + 6 = 20为 hidden 的值<input id="answer">,但将其保留14 + 6 = <span class="emphasis">20</span>在表中。

编辑澄清#4 ...

关键是任何<span>具有类的 s 都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>

4

2 回答 2

2

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

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

Redu 解决了同样的问题。


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

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)通过方法从答案删除所有html-tags.replace()

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)使用.find()方法

selectorAnswer = $( 'tr.problem_display_work:last td:first' );
selectorAnswer.find( '.emphasis' ).contents().unwrap();
于 2016-06-23T04:30:55.787 回答
0

您的问题不清楚。我知道您想从表格的所有单元格中删除“强调”类。解决办法是..

$('tr.problem_display_work:last td:first').removeClass("emphasis");

于 2016-06-23T04:04:49.300 回答