简体   繁体   English

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

[英]How do I move all the li elements with specific css style to another ul?

I need to move all of the <li style="display:none;"> under a particular <ul id="bob"> into a different <ul id="cat"> . 我需要将特定<ul id="bob">下的所有<li style="display:none;"> <ul id="bob">到另一个<ul id="cat"> In performing this move, I need all the classes, ids, and css styling to be preserved, and obviously the content as well. 在执行此移动时,我需要保留所有类,ID和CSS样式,并且显然也需要内容。

Easy one-liner: 简单的单线程:

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

will move all ul#bob > li which are hidden, which is a superset of ul#bob > li which have style="display:none;" 将移动所有隐藏的ul#bob > li ,这是ul#bob > li的超集,其中style="display:none;" , since: ,自:

Elements can be considered hidden for several reasons: 元素可被视为隐藏,原因如下:

  • They have a CSS display value of none . 它们的CSS display值为none
  • They are form elements with type="hidden" . 它们是type="hidden"表单元素。
  • Their width and height are explicitly set to 0. 它们的宽度和高度显式设置为0。
  • An ancestor element is hidden, so the element is not shown on the page. 隐藏了一个祖先元素,因此该元素不会显示在页面上。

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


If you really want only those elements whose style attribute is display: none; 如果你真的 想要那些style属性为display: none;元素display: none; , I'd do it with .filter() and a regexp: ,我用.filter()和正则表达式做到这.filter()

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

The regexp is case- and (mostly) whitespace-insensitive, and allows the semicolon to be optional, which is valid CSS. 正则表达式是case-和(大多数)空格不敏感,并允许分号是可选的,这是有效的CSS。 If you don't care about either of those you can git-r-dun with a one-liner, as before: 如果你不关心其中任何一个,你可以像以前一样使用单线git-r-dun:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何获取所有LI UL元素ID值并将它们放置在JavaScript数组中? - How do I get all the LI UL elements ID value and place them in a JavaScript array? 我如何找到最后一个 <li></li> 在每一个 <ul></ul> (前端,html,css,jquery) - How do i find the last <li></li> in every <ul></ul> (Front-end, html, css , jquery) 如何关闭同级 LI Elements 的 UL? - How do I close the UL of sibling LI Elements? 如何在其CSS类的基础上将现有ul的所有li添加到另一个ul的底部 - How to add all li of existing ul to the bottom of another ul base on their css classes 我如何将 li 移动到 ul 的底部? - How would I move an li to the bottom of a ul? JQuery - 如何将li移动到ul中的另一个位置? (交换2里) - JQuery - How to move a li to another position in the ul? (exchange 2 li's) 如果我有多个 ul 并且所有 li 具有相同的类名,我如何获得特定于 ul 的 li - how can I get the the li specific to a ul if i have more than one ul and all the li have same classname 将 ul 中的所有 li 项移动到单独的 ul - Move all li items in a ul to a separate ul 即使在另一个 ul 下,如何获得 ul 的所有后代 li - How to get all decendant li of ul even under another ul 如何将所有ul和li保留在父li下:selected,但删除其他导航项 - How do I keep all ul and li's under a parent li:selected, but remove other navigation items
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM