从.html()中排除类的div [英] Excluding div with class from .html()

查看:126
本文介绍了从.html()中排除类的div的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我有:

$activity = $("#activity_contents").html();

是有办法排除列表项与类:

is there a way to exclude a list item with a class:

<li class="correct">Something</li>

感谢。

更新

我有一个拖放活动,该活动的原始状态使用:

I have a drag and drop activity where the reset button resets the activity to it's original state using:

<a href="#" id="reset_activity" class="activity_button" title="Clear my answers" >Reset</a>

jQuery

$activity = $("#activity_contents").html();

$("#activity #reset_activity").click(function(e) {
    e.preventDefault();
    $("#activity_contents").html($activity);
    refreshActivity();
});

但我不想要任何ul类与正确的类重置。这是可能吗?

But I don't want any ul li with a class of correct to reset. Is that even possible?

感谢

推荐答案

克隆元素,找到并删除 .correct 后代元素,然后使用 .end()重置查询并检索克隆的HTML:

One approach would be to clone the element, find and remove the .correct descendant element(s), then reset the query using .end() and retrieve the cloned HTML:

$("#activity_contents").clone().find('.correct').remove().end().html();

您也可以在不克隆元素的情况下执行此操作,但这将影响当前存在于DOM。上述方法不会从DOM中删除任何内容,而下面的方法将会:

You could also do this without cloning the elements, however that would affect the element that currently exists in the DOM. The above method wouldn't removing anything from the DOM whereas the one below would:

$("#activity_contents").find('.correct').remove().html();

但是,根据您的更新,您可以直接删除元素。没有必要使用 .html()方法:

However, based on your update, you could simply just remove the elements directly. There is no need to use the .html() method:

$("#activity #reset_activity").click(function(e) {
  e.preventDefault();
  $("#activity_contents .correct").remove();
  refreshActivity();
});

这篇关于从.html()中排除类的div的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆