2011-10-24 99 views
5

我在使用IE8獲取我的一個網站頁面時遇到了一些問題。它在IE9,Safari(Mac電腦&)& Firefox(Mac)中正常工作。我使用的是find(tag1).html(tag1)調用序列做了標題替換,但我得到在IE8以下錯誤,當我在IE腳本調試器調試它,這在html(tag2)功能:jQuery .find()錯誤:「意外調用方法或屬性訪問」

Unexpected call to method or property access

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中?

回答

1

<header>是一個HTML5標記,IE8不知道(IE9,但支持此標記)。既然你聲明瞭XHTML 1.0 transitional,我建議使用<h1>標籤,這在IE8中可以很好地工作。

4

爲了支持舊版本IE(8及以下版本)中的新HTML 5元素,有一個方便的技巧,它涉及在運行腳本之前創建一個虛擬元素。

因此,只需在您的頁面中調用document.createElement('header');即可解決問題,請參閱here

有關完整說明,this post提供了一個很好的解釋。

另外,html5shiv也是一個解決了其他元素的這個問題的項目。

相關問題