jQuery .find() 错误:“意外调用方法或属性访问”
我在使用 IE8 获取我的网站页面之一时遇到一些问题。它在 IE9、Safari(PC 和 Mac)和 Safari 中运行良好。火狐(苹果机)。我正在使用 find(tag1).html(tag1)
调用序列来进行标题替换,但是当我在 IE 脚本调试器中调试它时,我在 IE8 中收到以下错误,这在html(tag2)
函数:
意外调用方法或属性访问
find(tag1)
函数似乎返回封闭对象(即 #sidebar
),而不是嵌套对象 #sidebarheader< /code>,这会在稍后进行
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 中工作吗?
I'm having some problems with getting one of my site pages with IE8. It works fine in IE9, Safari (both PC & Mac) & Firefox (Mac). I'm using a find(tag1).html(tag1)
call sequence to do a title substitution, but I get the following error in IE8 when I debug it in the IE script debugger, and this in the html(tag2)
function:
Unexpected call to method or property access
The find(tag1)
function seems to return the enclosing object (i.e. #sidebar
), rather than the nested object #sidebarheader
, and this causes problems when later making the html(tag2)
call.
I've created a representative test case as follows:
<!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>
And here is the jsFiddle test case:
Has anybody a suggestion on how to get this to work in IE8?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
为了在旧版本的 IE(8 及更低版本)中支持新的 HTML 5 元素,有一个方便的技巧,其中涉及在运行脚本之前创建一个虚拟元素。
因此,只需在页面中调用
document.createElement('header');
即可解决问题,请参阅 这里。对于完整的解释,这篇文章很好地提供了解释。
此外,html5shiv 也是一个为其他元素解决此问题的项目。
In order to support the new HTML 5 elements in older versions of IE (8 and below), there's a handy trick, which involves creating a dummy element before running your script.
So, simply calling
document.createElement('header');
in your page will solve the problem, see here.For the full explanation, this post does a nice job of providing an explanation.
Also, html5shiv is a project that solves this problem for other elements too.
<标题>是一个 HTML5 标签,IE8 不知道它(但是 IE9 支持这个标签)。由于您声明 XHTML 1.0 过渡,我建议使用
标签,这在 IE8 中可以正常工作。
<header> is a HTML5 tag, which IE8 doesn't know about (IE9 however, supports this tag). Since you're declaring XHTML 1.0 transitional, I'd suggest using a <h1> tag instead, which will work just fine in IE8.