使用IE8获取我的网站页面时遇到一些问题。它在IE9,Safari(PC和Mac)和Firefox(Mac)中都可以正常运行。我正在使用find(tag1).html(tag1)
调用序列来进行标题替换,但是当我在IE脚本调试器中以及在html(tag2)
函数中对其进行调试时,在IE8中出现以下错误:
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中工作?
最佳答案
为了支持旧版IE(8及更低版本)中的新HTML 5元素,有一个方便的技巧,其中涉及在运行脚本之前创建一个哑元素。
因此,只需在页面中调用document.createElement('header');
即可解决此问题,请参阅here。
对于完整的解释,this post在提供解释方面做得很好。
另外,html5shiv是一个也可以解决其他元素问题的项目。
关于javascript - jQuery .find()错误: "Unexpected call to method or property access",我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/7881169/