javascript - 如何获取滚动条内可见div的id
问题描述
我有几个 div 包含在带有滚动条的主 div 中。这里我需要通过单击按钮获取滚动条内可见 div 的 id。我对此一无所知,任何人都可以帮我解决这个问题。here是下面的代码。
html
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<p>Demonstrate fadeIn() with different parameters.</p>
<button>Click to get the current visible id</button><br>
<div id="mainbody">
<div id="div1">it is a long established fact that a reader will be distracted by the readable content of a page when looking at its layout. The point of using Lorem Ipsum is that it has a more-or-less normal distribution of letters, as opposed to using 'Content here, content here', making it look like readable English. Many desktop publishing packages and web page editors now use Lorem Ipsum as their default model text, and a search for 'lorem ipsum' will uncover many web sites still in their infancy. Various versions have evolved over the years, sometimes by accident, sometimes on purpose (injected humour and the like).</div><br>
<div id="div2">it is a long established fact that a reader will be distracted by the readable content of a page when looking at its layout. The point of using Lorem Ipsum is that it has a more-or-less normal distribution of letters, as opposed to using 'Content here, content here', making it look like readable English. Many desktop publishing packages and web page editors now use Lorem Ipsum as their default model text, and a search for 'lorem ipsum' will uncover many web sites still in their infancy. Various versions have evolved over the years, sometimes by accident, sometimes on purpose (injected humour and the like).</div><br>
<div id="div3">it is a long established fact that a reader will be distracted by the readable content of a page when looking at its layout. The point of using Lorem Ipsum is that it has a more-or-less normal distribution of letters, as opposed to using 'Content here, content here', making it look like readable English. Many desktop publishing packages and web page editors now use Lorem Ipsum as their default model text, and a search for 'lorem ipsum' will uncover many web sites still in their infancy. Various versions have evolved over the years, sometimes by accident, sometimes on purpose (injected humour and the like).</div>
</div>
风格
#mainbody{
height:70px;
overflow:auto;
}
脚本
$(document).ready(function(){
$("button").click(function(){
});
});
解决方案
$("button").click(function(){
$("#mainbody").find("div:visible").each(function(){
alert($(this).attr('id'));
});
});
例子:
$(document).ready(function(){
$("button").click(function(){
$("#mainbody").find("div:visible").each(function(){
alert($(this).attr('id'));
});
});
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<p>Demonstrate fadeIn() with different parameters.</p>
<button>Click to get the current visible id</button><br>
<div id="mainbody">
<div id="div1">it is a long established fact that a reader will be distracted by the readable content of a page when looking at its layout. The point of using Lorem Ipsum is that it has a more-or-less normal distribution of letters, as opposed to using 'Content here, content here', making it look like readable English. Many desktop publishing packages and web page editors now use Lorem Ipsum as their default model text, and a search for 'lorem ipsum' will uncover many web sites still in their infancy. Various versions have evolved over the years, sometimes by accident, sometimes on purpose (injected humour and the like).</div><br>
<div id="div2">it is a long established fact that a reader will be distracted by the readable content of a page when looking at its layout. The point of using Lorem Ipsum is that it has a more-or-less normal distribution of letters, as opposed to using 'Content here, content here', making it look like readable English. Many desktop publishing packages and web page editors now use Lorem Ipsum as their default model text, and a search for 'lorem ipsum' will uncover many web sites still in their infancy. Various versions have evolved over the years, sometimes by accident, sometimes on purpose (injected humour and the like).</div><br>
<div id="div3">it is a long established fact that a reader will be distracted by the readable content of a page when looking at its layout. The point of using Lorem Ipsum is that it has a more-or-less normal distribution of letters, as opposed to using 'Content here, content here', making it look like readable English. Many desktop publishing packages and web page editors now use Lorem Ipsum as their default model text, and a search for 'lorem ipsum' will uncover many web sites still in their infancy. Various versions have evolved over the years, sometimes by accident, sometimes on purpose (injected humour and the like).</div>
</div>
推荐阅读
- php - mysql join查询如何使用php访问两个表数据
- javascript - 如何在浏览器中使用后退和前进按钮浏览页面而不丢失 NextJS 中的页面状态?
- javascript - 垂直排序父母,水平排序孩子
- php - 一页有两个分页 YII2
- amp-html - 我们可以在 amp 故事网格层标签中使用 amp youtube 标签吗
- python - 从导入的 XML 文件访问列表索引
- wordpress - 在 Wordpress 5.0 中创建页面模板
- php - Ajax 文件上传“尝试加载资源时发生错误”和网络连接在 Safari 中丢失
- java - 无法在控制台模式下通过 java exec 命令运行 JMeter 5.0
- swift - 通过指定类型将“具有关联类型的协议”转换为常规协议