javascript - 如何将具有特定 css 样式的所有 li 元素移动到另一个 ul?

标签 javascript jquery html css html-lists

我需要移动所有 <li style="display:none;">在特定的 <ul id="bob"> 下进入不同的<ul id="cat"> .在执行此移动时,我需要保留所有类、id 和 css 样式,显然还需要保留内容。

最佳答案

简单的一行:

$('#bob > li:hidden').appendTo('#cat');

将移动所有隐藏的 ul#bob > li,它是 ul#bob > li 的超集,它具有 style="display:none ;",因为:

Elements can be considered hidden for several reasons:

  • They have a CSS display value of none.
  • They are form elements with type="hidden".
  • Their width and height are explicitly set to 0.
  • An ancestor element is hidden, so the element is not shown on the page.

http://api.jquery.com/hidden-selector


如果您真的想要那些style属性为display: none;的元素,我会用 .filter() 做和一个正则表达式:

var pattern = /^\s*display:\s*none\s*;?\s*$/gi;
$('#bob > li').filter(function ()
{
    return pattern.test($(this).prop('style'));
}).appendTo('#cat');

正则表达式不区分大小写和(大部分)空格,并允许分号是可选的,这是有效的 CSS。如果您不关心其中任何一个,您可以像以前一样使用单行 git-r-dun:

$('#bob > li[style="display:none;"]').appendTo('#cat');

关于javascript - 如何将具有特定 css 样式的所有 li 元素移动到另一个 ul?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7396276/

相关文章:

javascript - 从 Node-JS 将焦点设置到 Windows 应用程序

javascript - jQuery 开关按钮开/关应设置不同的数据状态和 Onclick 功能

javascript - 从 x 轴重绘问题中删除数据点

html - Bootstrap onhover 用于多级菜单

javascript - 使简单的 jquery 'hover' 工作时出现问题

jquery - 使用 Jquery 在 xml 中查找节点

javascript - 无法为 Summernote 所见即所得编辑器设置值

javascript - 触发单选按钮单击并设置选中的属性问题

javascript - 按钮标签在 IE 7 中不起作用

javascript - jquery-steps "Next"和 "Previous"按钮上的图标