jquery - 删除不包含特定类的html元素

标签 jquery jquery-selectors

我正在尝试删除处于非事件状态的 li,但无法使其工作:

var html = $("#myjqgriddiv").remove("ul li:not(.ui-state-active)").html();

我有html,我想删除不包含类.ui-state-active的li

在 li 上方的代码之后,.ui-state-active 仍然存在。

需要帮助。

更新:

我打算使用 window.open("","Print")/document.write 打印 html

@tvanfosson 答案有效,但还有一点小事情,第一次修改后留下的单个 li 我也想编辑,我想从其 a 子元素中删除属性“href”

var html = $('#myjqgriddiv') // select div
                .find('ul li:not(.ui-state-active)') // select elements to remove from div
                .remove()  // remove the matched elements
                .end() // revert back to the originally selected elements
                .html(); // and get the HTML


html = $(html).find("ul li.ui-state-active a)").removeAttr("href").html(); // not working?!

最佳答案

remove 的参数充当过滤器,而不是选择器。由于您只选择了 id mygqgriddiv 的一个元素,因此过滤器不会删除任何内容。尝试一下。

 var html = $('#myjqgriddiv') // select div
                .find('ul li:not(.ui-state-active)') // select elements to remove from div
                .remove()  // remove the matched elements
                .end() // revert back to the originally selected elements
                .find("ul li.ui-state-active a)") // now get the active ones
                .removeAttr("href") // remove the href attribute
                .end() // back to the original selection
                .html(); // and get the HTML

关于jquery - 删除不包含特定类的html元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7375614/

相关文章:

javascript - 设置不透明度动画时,Jquery 滚动页眉会中断

jquery - 寻找一个不错的 jquery 拖放树插件实现

javascript - 为什么 onresize 在 IE8 中不应在低缩放级别下触发?

jquery选择没有事件类的元素

jquery - 当我单击数据表中的下一页时,我的 jquery 选择器不再工作

javascript - 在 document.ready() 中异步加载外部 JS 文件

javascript - 将固定大小的图像限制在 div 容器内?

javascript - 使用 jquery 插入动态 HTML 后单击不起作用

jQuery 索引和选择器 :lt and :gt

javascript - 使用 jquery 将行值设为另一个文本