JQuery:如果 div 可见 [英] JQuery: if div is visible
问题描述
我使用 JS 作为更改我创建的 SPA 内容的一种方式.当我按下一个按钮来更改内容时,HTML 从此更改:
为此:
现在我的 SPA 的一部分有一个包含多个复选框的 div,每个复选框代表一个 div,因此当我按下提交按钮时,应该显示的下一个 div 将是复选框列表中的第一个项目已选中.
我想知道 JQuery 中是否有一种方法可以让代码几乎检测"哪个 div 现在可见.像这样:
if($('#selectDiv').isVisible()){//与这个div关联的JS代码.}
有什么建议吗?
解决方案 您可以使用 .is(':visible')
<块引用>选择所有可见的元素.
例如:
if($('#selectDiv').is(':visible')){
此外,您可以通过以下方式获取可见的 div:
$('div:visible').callYourFunction();
现场示例:
console.log($('#selectDiv').is(':visible'));console.log($('#visibleDiv').is(':visible'));
#selectDiv {显示:无;}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script><div id="selectDiv"></div><div id="visibleDiv"></div>
I'm using JS as a way of changing the content of an SPA I'm creating. When I press a button to change the content the HTML changes from this:
<div id="selectDiv" style="display: none;">
to this:
<div id="selectDiv" style>
Now part of my SPA has a div that contains a number of checkboxes, each representing a div, and so when I press the submit button, the next div that should be displayed will be the first item in the checkbox list that was selected.
I'm wondering if there's a way in JQuery for the code to "almost detect" which div is now visible. something like this:
if($('#selectDiv').isVisible()){
//JS code associated with this div.
}
Any suggestions?
解决方案 You can use .is(':visible')
Selects all elements that are visible.
For example:
if($('#selectDiv').is(':visible')){
Also, you can get the div which is visible by:
$('div:visible').callYourFunction();
Live example:
console.log($('#selectDiv').is(':visible'));
console.log($('#visibleDiv').is(':visible'));
#selectDiv {
display: none;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="selectDiv"></div>
<div id="visibleDiv"></div>
这篇关于JQuery:如果 div 可见的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文
相关文章
- JQuery:如果div是可见的;
- Jquery使DIV可见/不可见;
- JQuery的,如果一个div在页面上可见?;
- 使用JQuery切换div可见性;
- 使用jQuery检查div是否可见;
- 使用jQuery获取div的可见高度;
- jQuery-检查子div是否可见;
- div显示如果阻止jquery;
- jQuery,如果div不是id;
- jQuery事件处理程序:div变为可见/隐藏;
- 在jquery中查找当前可见的div;
- 使用jquery使div可见性为false;
- jQuery,如果div ID有孩子;
- 如何使用jQuery将div CSS的可见性更改为可见;
- 如果子div为空,则jQuery隐藏父div;
- 如果DIV没有类"x",则为jQuery;
- 如果Div不存在则jQuery;
- jQuery:如何在div可见时为div绑定一个事件?;
- JQuery hide Class如果其他类可见或显示;
- jQuery height()在可见div上返回0 - 为什么?;
- 当div可见时触发动作的jQuery事件;
- jQuery如何在单击时隐藏其他可见的div;
- 当 div 可见时触发动作的 jQuery 事件;
- jQuery检查元素在可滚动div中是否可见;
- jquery .show()不会透露具有隐藏可见性的div;
前端开发最新文章
- 为什么Chrome(在Electron内部)突然重定向到chrome-error:// chromewebdata?;
- 错误102(net :: ERR_CONNECTION_REFUSED):服务器拒绝连接;
- 如何解决'重定向已被CORS策略阻止:没有'Access-Control-Allow-Origin'标题'?;
- 如何处理“Uncaught(in promise)DOMException:play()失败,因为用户没有首先与文档交互。”在桌面上使用Chrome 66?;
- 警告:添加非被动事件侦听器到滚动阻塞'touchstart'事件;
- 如何在浏览器中播放.TS文件(视频/ MP2T媒体类型)?;
- 此请求已被阻止;内容必须通过HTTPS提供;
- 资源解释为样式表,但转换为MIME类型text / html(似乎与web服务器无关);
- 通过HTTPS加载页面但请求不安全的XMLHttpRequest端点;
- 拒绝从执行脚本'*',因为它的MIME类型(“应用/ JSON')不是可执行文件,并严格MIME类型检查被启用。;