Javascript(如何在IE7浏览器中检查Id是否存在)

Javascript (how to check Id exist or not in IE7 Browser)

本文关键字:Id 检查 是否 存在 浏览器 IE7 Javascript      更新时间:2023-09-26

如何查看IE7浏览器中Id是否存在?下面是我的代码,但它不能在IE7中成功运行。

if(document.getElementById('Username') != null) { 
   alert("In"); 
}

当运行这段代码时,即使Id ('Username)不存在,也会显示警告框。

删除!= null部分,如下所示:

if(document.getElementById('Username'))
{
    alert("In");
}

使用jQuery

if ($.browser.msie  && parseInt($.browser.version) == 7) {
  if ( $("#Username").length > 0 ) {
    //do something
    }
}

首先,如果要检查id是否存在,则

if(document.getElementById('Username'))

但是如果你检查是否(我假设它是一个文本框)文本框是空的或者不是,那么

if(document.getElementById('Username').text ==null)