如何使用 . hide () 、.show () 或.toggle () 来切换元素的可见度 ?
如果元素可见或隐藏, 我如何测试 ?
如何使用 . hide () 、.show () 或.toggle () 来切换元素的可见度 ?
如果元素可见或隐藏, 我如何测试 ?
当前回答
而不是为每个元素写入事件, 请这样做 :
$('div').each(function(){
if($(this).css('display') === 'none'){
$(this).css({'display':'block'});
}
});
您也可以在以下输入中使用:
$('input').each(function(){
if($(this).attr('type') === 'hidden'){
$(this).attr('type', 'text');
}
});
其他回答
毕竟,没有一个例子适合我, 所以我写了我自己的。
测试( 不支持互联网探索过滤器: alpha) :
a) 检查文档是否隐藏
b) 检查元素是否为零宽度/高度/不透明或显示:无/可见度:内嵌样式中隐藏
c) 检查元素的中心(也因为它比测试每个像素/角的速度快)是否被其他元素(和所有祖先,例如:溢出:隐藏/滚动/一个元素在另一个元素之上)或屏幕边缘所隐藏
d) 检查元素是否为零宽度/高度/不透明或显示:无/可见度:以计算样式隐藏(在所有祖先中)
测试在
和机器人4.4(本地浏览器/铬/火狐)、火狐(风窗/摩克)、铬(风窗/摩克)、歌剧(风窗预示/摩克 Webkit)、互联网探索者(互联网探索者5-11文档模式+虚拟机器上的互联网探索者8)和Safari(风窗/摩克/奥兹)。
var is_visible = (function () {
var x = window.pageXOffset ? window.pageXOffset + window.innerWidth - 1 : 0,
y = window.pageYOffset ? window.pageYOffset + window.innerHeight - 1 : 0,
relative = !!((!x && !y) || !document.elementFromPoint(x, y));
function inside(child, parent) {
while(child){
if (child === parent) return true;
child = child.parentNode;
}
return false;
};
return function (elem) {
if (
document.hidden ||
elem.offsetWidth==0 ||
elem.offsetHeight==0 ||
elem.style.visibility=='hidden' ||
elem.style.display=='none' ||
elem.style.opacity===0
) return false;
var rect = elem.getBoundingClientRect();
if (relative) {
if (!inside(document.elementFromPoint(rect.left + elem.offsetWidth/2, rect.top + elem.offsetHeight/2),elem)) return false;
} else if (
!inside(document.elementFromPoint(rect.left + elem.offsetWidth/2 + window.pageXOffset, rect.top + elem.offsetHeight/2 + window.pageYOffset), elem) ||
(
rect.top + elem.offsetHeight/2 < 0 ||
rect.left + elem.offsetWidth/2 < 0 ||
rect.bottom - elem.offsetHeight/2 > (window.innerHeight || document.documentElement.clientHeight) ||
rect.right - elem.offsetWidth/2 > (window.innerWidth || document.documentElement.clientWidth)
)
) return false;
if (window.getComputedStyle || elem.currentStyle) {
var el = elem,
comp = null;
while (el) {
if (el === document) {break;} else if(!el.parentNode) return false;
comp = window.getComputedStyle ? window.getComputedStyle(el, null) : el.currentStyle;
if (comp && (comp.visibility=='hidden' || comp.display == 'none' || (typeof comp.opacity !=='undefined' && comp.opacity != 1))) return false;
el = el.parentNode;
}
}
return true;
}
})();
如何使用 :
is_visible(elem) // boolean
用于检查元素是否可见、不显示,甚至不透明度水平的扩展功能
如果元素不可见, 它返回错误 。
function checkVisible(e) {
if (!(e instanceof Element)) throw Error('not an Element');
const elementStyle = getComputedStyle(e);
if (elementStyle.display === 'none' || elementStyle.visibility !== 'visible' || elementStyle.opacity < 0.1) return false;
if (e.offsetWidth + e.offsetHeight + e.getBoundingClientRect().height +
e.getBoundingClientRect().width === 0) {
return false;
}
const elemCenter = {
x: e.getBoundingClientRect().left + e.offsetWidth / 2,
y: e.getBoundingClientRect().top + e.offsetHeight / 2
};
if (elemCenter.x < 0 || elemCenter.y < 0) return false;
if (elemCenter.x > (document.documentElement.clientWidth || window.innerWidth)) return false;
if (elemCenter.y > (document.documentElement.clientHeight || window.innerHeight)) return false;
let pointContainer = document.elementFromPoint(elemCenter.x, elemCenter.y);
do {
if (pointContainer === e) return true;
} while (pointContainer = pointContainer.parentNode);
return false;
}
您需要检查... 显示和可见度 :
if ($(this).css("display") == "none" || $(this).css("visibility") == "hidden") {
// The element is not visible
} else {
// The element is visible
}
如果我们自动检查$( thiss) 。 is (“: 可见”) , jquery check for both the things 自动检查 。
当对 : hidden 选择器在 jquery 中测试元素时,应考虑一个绝对定位元素可以被确认为隐藏,尽管其子元素是可见的。
虽然仔细看一看拼图文件能提供相关资料:
由于以下几个原因可视为隐藏要素:[.]其宽度和高度明确定为0。 [.]
因此,这实际上对框模型和元素的计算样式是有道理的。即使宽度和高度没有被明确设定为0,它们也可以被隐含地设定为0。
以下列实例为例:
控制台. log( $ ('. foo') ) ; // true control. org ($ ('.bar') ) ; / foo { 位置 : 绝对 : left: 10px; 顶层 : ff00; 位置 : 绝对 : 10px; 顶层 : 10px; 宽度 : 20px; 高度 20px; 底层 : 0000ff; } < 上层 src=" https://ajax.gogleapis.com/ahax/libs/jquery/ 2. 1.1/jquery.min.js. > & & lt;/ statict> & & & div>
jquery 3. x 的更新:
与 jquery 3 相比, 所述的行为将改变元素, 如果有任何布局框, 包括宽度为零和(或)高度的布局框, 则将被视为可见的元素 。
3 0.0 - 阿尔法1: jdidfidd with jquery 3. 0.0 - 阿尔法1:
http://jsfiddle.net/pM2q3/7/
同样的javascript代码 将会有这个输出 :
console.log($('.foo').is(':hidden')); // false
console.log($('.bar').is(':hidden')); // false
通常当检查某物是否可见时, 您会立即直接去做其他事情。 jquery 绑链很容易做到 。
所以,如果您有一个选择器, 并且您想要在它上执行某些动作, 只有当它为可见或隐藏时, 您可以使用过滤器 (“ : 可见 ” ) 或过滤器 (“ 隐藏 ” ) , 然后将它与您想要采取的行动连接起来 。
而不是这样声明:
if ($('#btnUpdate').is(":visible"))
{
$('#btnUpdate').animate({ width: "toggle" }); // Hide button
}
或效率更高,但更丑陋:
var button = $('#btnUpdate');
if (button.is(":visible"))
{
button.animate({ width: "toggle" }); // Hide button
}
您可以在一条线内完成全部任务:
$('#btnUpdate').filter(":visible").animate({ width: "toggle" });