[英]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 ofnone
.它们的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.