简体   繁体   English

JavaScript中元素的可见性和存在性

[英]visibility and existence of an element in javascript

我需要知道这是否是检查javascript中某个元素的可见性和是否存在的好习惯

if(document.getelementbyid("id") && document.getelementbyid("id").style.visibility=="visible")){}

Checking existence of element using document.getElementById is good practice since JavaScript will not throw an error if element does not exists on the page. 使用document.getElementById检查元素的存在是一种好习惯,因为如果页面上不存在元素,JavaScript不会抛出错误。

However, checking visibility is entirely dependent on the coding conditions. 但是,检查visibility完全取决于编码条件。

Correct syntax: 正确的语法:

if(document.getElementById("id") && document.getElementById("id").style.visibility == "visible"){}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM