从.html()中删除带有类的div
如果我有:
$activity = $("#activity_contents").html();
有没有办法用类排除列表项:
Something
来自#activity_contents div中的html内容显示?
谢谢。
更新
我有一个拖放活动,其中重置按钮使用以下命令将活动重置为其原始状态:
Reset
jQuery的
$activity = $("#activity_contents").html(); $("#activity #reset_activity").click(function(e) { e.preventDefault(); $("#activity_contents").html($activity); refreshActivity(); });
但我不希望任何一个正确的类别重置。 这有可能吗?
谢谢
一种方法是克隆元素,查找和删除.correct
descendant元素,然后使用.end()
重置查询并检索克隆的HTML:
$("#activity_contents").clone().find('.correct').remove().end().html();
您也可以在不克隆元素的情况下执行此操作,但这会影响DOM中当前存在的元素。 上面的方法不会从DOM中删除任何内容,而下面的方法会:
$("#activity_contents").find('.correct').remove().html();
但是,根据您的更新,您只需直接删除元素即可。 无需使用.html()
方法:
$("#activity #reset_activity").click(function(e) { e.preventDefault(); $("#activity_contents .correct").remove(); refreshActivity(); });
使用find()在类中找到特定的’li’并将其display属性设置为’none’。它只是隐藏了li。 如果你想删除那个li然后使用
$("#activity_contents").find('li.correct').remove();
HTML代码
test000 - Something1
- Something2
- Something3
test111