【问题标题】:JS: How to make document.getElementById cross-browser?JS:如何使 document.getElementById 跨浏览器?
【发布时间】:2016-06-03 00:48:47
【问题描述】:

document.getElementById 似乎不适用于所有浏览器(我指的是一些旧浏览器),我相信有些开发人员不知道这一点。

你会建议什么解决方案让它跨浏览器?

谢谢

【问题讨论】:

  • document.getElementById 实际上是为数不多的适用于各种浏览器的东西之一,基本上是过去 几年 年的任何东西,包括 IE6。您尝试支持哪些特定浏览器但未实现它?
  • 请指定浏览器,谢谢
  • 如果 document.getElementById 不受支持,请不要为该浏览器使用 Javascript。
  • IE5 Mac,一位同事告诉我的。而对于更多c这个链接请在下面的答案中由Vinegar分享。 webdeveloper.com/forum/showthread.php?t=172374 有了那个链接,问题似乎就解决了。谢谢大家
  • IE5/Mac 支持getElementById() 我上次使用它。再说一次,我最后一次使用它是大约五年前,我是 Mac 用户。在许多方面,IE5/Mac 甚至比 IE6/Win 都拥有更好的 DOM 支持。但是,恕我直言,现在真的不值得支持它,如果你这样做了,你应该更担心 CSS 兼容性:它实现了 CSS2,而不是现代浏览器支持的 CSS2.1,并且有一些重要的区别。

标签: javascript html dom


【解决方案1】:

如果 document.getElementById 不起作用,那么:

  • 您做错了(无效的 HTML、试图访问名称而不是 ID 等)

  • 您正在使用 Netscape 4.x 和 Internet Explorer 4.x

应对这个时代的浏览器的三种方式。

  • 告诉人们升级。对于用户和作者来说,它们是无人维护的、充满安全漏洞的噩梦。
  • Build on stuff that works 并确保您的 JS 在尝试使用它们之前检查 getElementById 和朋友的存在 (if (!document.getElementById) { return false; /* Insufficient DOM to bother with JS here */ })
  • 了解document.alldocument.layers

【讨论】:

    【解决方案2】:

    你确定不是this kind of problem?看看它很有趣,我以前不知道。

    但是,为了补充 David Dorward 已经提出的建议,您可以编写如下所示的函数。

    function getElement (id) {
    
      if (document.getElementById) {
        return document.getElementById(id);
      }
    
      else if (document.all) {
        return window.document.all[id];
      }
    
      else if (document.layers) {
        return window.document.layers[id];
      }
    } 
    

    【讨论】:

      【解决方案3】:
      getElemID(obj){
      
      if(document.getElementByID){
       return document.getElementByID(obj);
      }
      
       else if (document.all){
        return document.all[obj];
        }
      
        else if (document.layers){
           return  document.layers[obj];
           }
      
        else {
             alert("Could not find support");
             return false;
             }
      }
      

      【讨论】:

        【解决方案4】:
        function getDOM() {
            if (document.getElementById) {
                return document.getElementById; 
            }
        
            var window_document = window.document || {};
            var elements = window_document.all || window_document.layers;
            if(elements) {
                return function(x) { return elements[x]; }
            }
        
            // everything failed
            throw new InternalError('No means to "getElementById"');
        }
        

        ...然后

        var getElementById;
        try {
            getElementById = getDOM();
        } catch(err) {
            alert(err);
        }
        // implicit 0K
        var oHTMLElement = getElementById('#main');
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2014-08-06
          • 2011-08-03
          • 1970-01-01
          • 1970-01-01
          • 2021-04-12
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多