选中复选框时,在表数据上附加和前置<s>标记(删除)

时间:2015-11-20 16:37:17

标签: javascript jquery html

当我点击我的复选框时,我正在尝试使用jQuery将类“tableEntry”中的所有内容转换为删除线文本(使用“s”标记)。我目前无法将标签添加到“tableEntry”类中显示的文本中。我怎样才能使用jQuery实现这一目标?

任何帮助将不胜感激,提前谢谢!

HTML:

<tr> 
  <td><input type="checkbox" /></td>
  <td><span class="tableEntry">Walk the Dog</span></td>
</tr>

jQuery到目前为止:

$(document).ready(function () {
  $(document).on("change", "input:checkbox", function () {
    // ...
  });
});

1 个答案:

答案 0 :(得分:1)

最好的方法是使用CSS。

您可以在元素上切换一个类,并使用text-decoration: line-through来实现此目的。

Example Here

$(document).on("change", "input:checkbox", function () {
  $(this).parent().next().find('span').toggleClass('strike');
});
.strike {
  text-decoration: line-through;
}

但是如果你必须用<s>标签包装/解包该元素(如你的问题所示),那么你可以使用以下内容:

Example Here

$(document).on("change", "input:checkbox", function () {
  var $span = $(this).parent().next().find('span');

  this.checked ? $span.wrap('<s></s>') : $span.unwrap();
});
相关问题