Ajax加载div,CSS的一部分无法正常工作

Syl*_*lph 5 javascript css ajax

我正在使用ajax加载div内容,但div内容没有采用页面的CSS.

示例: - 此链接将加载到

<a href="#" onclick="javascript:loadAjax('test.html')">Test</a> 

<div id="result">
<table class="tablesorter">
<thead>
    <tr>
        <th>Header 1</th><th>Header 2</th>
    </tr>
</thead>
<tbody>

    <tr><td>Record 1</td><td>Desc 1</td></tr>
</tbody>    
</table>
</div>
Run Code Online (Sandbox Code Playgroud)

在我的CSS中:

table.tablesorter thead tr th, table.tablesorter tfoot tr th {
    background-color: #e6EEEE;
    border: 1px solid #FFF;
    font-size: 8pt;
    padding: 4px;
}

table.tablesorter thead tr .header {
    background-image: url(bg.gif);
    background-repeat: no-repeat;
    background-position: center right;
    cursor: pointer;
}
Run Code Online (Sandbox Code Playgroud)

在我的test.html中,它是具有不同记录的相同表:

<table class="tablesorter">
    <thead>
        <tr>
            <th>Header 1</th><th>Header 2</th>
        </tr>
    </thead>
    <tbody>

        <tr><td>Record 2</td><td>Desc 2</td></tr>
    </tbody>    
    </table>
Run Code Online (Sandbox Code Playgroud)

我面临的问题是在"test.html"加载之前,CSS很好.但是在点击了假设加载test.html的链接之后,CSS背景仍然显示,但"光标:指针"和"背景图像"不再有效.

我该怎么做才能让它发挥作用?提前致谢!

在loadAjax代码中添加:

   var http_request = false;
   function loadAjax(url, parameters) {
      http_request = false;
      if (window.XMLHttpRequest) { // Mozilla, Safari,...
         http_request = new XMLHttpRequest();
         if (http_request.overrideMimeType) {
            // set type accordingly to anticipated content type
            //http_request.overrideMimeType('text/xml');
            http_request.overrideMimeType('text/html');
         }
      } else if (window.ActiveXObject) { // IE
         try {
            http_request = new ActiveXObject("Msxml2.XMLHTTP");
         } catch (e) {
            try {
               http_request = new ActiveXObject("Microsoft.XMLHTTP");
            } catch (e) {}
         }
      }
      if (!http_request) {
         alert('Cannot create XMLHTTP instance');
         return false;
      }
      http_request.onreadystatechange = alertContents;
      http_request.open('GET', url + parameters, true);
      http_request.send(null);
   }

   function alertContents() {
      if (http_request.readyState == 4) {
     // alert(http_request.status);
         if (http_request.status == 200) {
            //alert(http_request.responseText);
            result = http_request.responseText;
            document.getElementById('result').innerHTML = result;            
         } else {
            alert('There was a problem with the request.');
         }
      }
   }
Run Code Online (Sandbox Code Playgroud)

Ban*_*zor 3

尝试将相同的 css 添加到 test.html 文件。如果您实际上使用 iframe 或将一个页面嵌入到另一个页面中,则 CSS 将不会级联到嵌入的页面中。它被呈现为自己的文档。

更新:看起来您可能需要向行中的第一个单元格添加一个类以使其具有样式。test.html 中没有任何由 CSS 第二部分设置样式的元素,因为它不匹配任何元素。

<table class="tablesorter">
    <thead>
        <tr>
            <th>Header 1</th><th>Header 2</th>
        </tr>
    </thead>
    <tbody>

        <tr><td class="header">Record 2</td><td>Desc 2</td></tr>
    </tbody>    
    </table>
Run Code Online (Sandbox Code Playgroud)