【问题标题】:jQuery .find() error: "Unexpected call to method or property access"jQuery .find() 错误:“意外调用方法或属性访问”
【发布时间】:2011-10-25 04:05:05
【问题描述】:

我在使用 IE8 获取我的网站页面之一时遇到了一些问题。它在 IE9、Safari(PC 和 Mac)和 Firefox (Mac) 中运行良好。我正在使用find(tag1).html(tag1) 调用序列来进行标题替换,但是当我在 IE 脚本调试器中调试它时,在 IE8 中出现以下错误,而这在 html(tag2) 函数中:

对方法或属性访问的意外调用

find(tag1) 函数似乎返回封闭对象(即#sidebar),而不是嵌套对象#sidebarheader,这会在以后调用html(tag2) 时导致问题。

我创建了一个有代表性的测试用例如下:

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>JQuery .find() test case</title> 

    <script type="text/javascript" src="http://code.jquery.com/jquery-1.6.4.js"></script>
    <script  type="text/javascript">
        function UpdateHeader() { 
           $('#sidebar').find('header').html("New Title"); // IE8, nesting div's in the find fct. will not discover the child div
        }

        document.ready = UpdateHeader;
    </script>
</head>

<body>
      <div style="height: 400px; width: 390px">

          <div id="jqm-home">
            <div id="page">
                <div id="sidebar">
                    <div id="sidebarheader">
                        <header>Old Title</header>
                    </div>
                </div>
            </div>
          <p onclick="UpdateHeader();">Click to update title</p>   
          </div>
      </div>     
</body>            
</html>

这里是 jsFiddle 测试用例:

http://jsfiddle.net/bnmcK/21/

有没有人建议如何让它在 IE8 中工作?

【问题讨论】:

    标签: javascript jquery internet-explorer-8


    【解决方案1】:

    为了在旧版本的 IE(8 及以下)中支持新的 HTML 5 元素,有一个方便的技巧,即在运行脚本之前创建一个虚拟元素。

    因此,只需在您的页面中调用document.createElement('header'); 即可解决问题,请参阅here

    对于完整的解释,this post 很好地提供了解释。

    另外,html5shiv 也是一个为其他元素解决此问题的项目。

    【讨论】:

      【解决方案2】:

      是一个 HTML5 标签,IE8 不知道它(IE9 支持这个标签)。由于您声明 XHTML 1.0 过渡,我建议改用

      标记,这在 IE8 中可以正常工作。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-08-13
        • 2011-01-21
        • 1970-01-01
        • 1970-01-01
        • 2013-04-25
        • 2017-07-29
        相关资源
        最近更新 更多