Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/74.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ssis/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 溢出隐藏元素_Javascript_Jquery_Css_Dom - Fatal编程技术网

Javascript 溢出隐藏元素

Javascript 溢出隐藏元素,javascript,jquery,css,dom,Javascript,Jquery,Css,Dom,我想知道如何访问溢出的元素。我想把他们的内容复制到另一个div或别的什么地方。 假设我有一个ul,有5个li元素,但只有两个是可见的。所以如何在另一个div中获取其他3个元素?您必须计算某个元素是否可见,并为此进行假设。下面是一个简单的例子。它假定该列表是一个传统列表(即每个项目位于下一个项目的下方)。然后,它使用计算来确定偏移量是否大于容器的高度。您可以对此进行调整,以查看某些内容是部分可见还是完全可见 return this.offsetTop + $(this).height() >

我想知道如何访问溢出的元素。我想把他们的内容复制到另一个div或别的什么地方。
假设我有一个ul,有5个li元素,但只有两个是可见的。所以如何在另一个div中获取其他3个元素?

您必须计算某个元素是否可见,并为此进行假设。下面是一个简单的例子。它假定该列表是一个传统列表(即每个项目位于下一个项目的下方)。然后,它使用计算来确定偏移量是否大于容器的高度。您可以对此进行调整,以查看某些内容是部分可见还是完全可见

return this.offsetTop + $(this).height() > height;
表示在项目不完全可见时应用效果。要不移动部分可见的项目,请将其更改为:

return this.offsetTop > height;
完整示例:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.0/jquery.min.js"></script>
<script type="text/javascript">
$(function() {
  var height = $("#container").height();
  $("#container li").filter(function() {
    return this.offsetTop + $(this).height() > height;
  }).wrapAll("<ul>").parent().appendTo("#overflow");
});
</script>
<style type="text/css">
div, ul, li { border: 0 none; padding: 0; margin: 0; }
#container { background: yellow; margin-bottom: 20px; height: 50px; overflow: hidden; }
#overflow { background: #CCC; }
</style>
</head>
<body>
<div id="container">
<ul>
<li>one</li>
<li>two</li>
<li>three</li>
<li>four</li>
<li>five</li>
</div>
<div id="overflow">
</div>
</body>
</html>

$(函数(){
变量高度=$(“#容器”).height();
$(“#容器li”).filter(函数(){
返回this.offsetTop+$(this).height()>height;
}).wrapAll(“
    ”).parent().appendTo(“#溢出”); }); div,ul,li{边框:0无;填充:0;边距:0;} #容器{背景:黄色;边距底部:20px;高度:50px;溢出:隐藏;} #溢出{背景:#CCC;}
    • 一个
    • 两个
一个更普遍的解决方案将更加乏味。您必须满足“视口”左侧、右侧、上方或下方的项目