Ajax加载内容,jquery插件不工作

发布于 2024-09-02 08:10:09 字数 3739 浏览 7 评论 0原文

我有一个调用 ajax 来加载内容的链接,加载内容后,我的 jquery 函数不再工作

这是我的 HTML

 <a href="#" onclick="javascript:makeRequest('content.html','');">Load Content</a>
    <span id="result">
 <table id="myTable" valign="top" class="tablesorter">
        <thead>
          <tr>
            <th>Title 1</th>
            <th>Level 1</th>
            <th>Topics</th>
            <th>Resources</th>
          </tr>
        </thead>
        <tbody>
          <tr>
            <td>Example title 1</td>
            <td>Example level 1</td>
          </tr>
          <tr>
            <td>Example title 2</td>
            <td>Example level 2</td>
          </tr>
        </tbody>
      </table>
</span>

该表使用来自 http://tablesorter.com/docs/ ajax内容加载后,将显示另一组不同数据的表格。但是,排序不再起作用。

这是我的 ajax 脚本,用于加载内容:

  var http_request = false;
   function makeRequest(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.');
         }
      }
   }

知道如何在加载内容后让 jquery 插件工作吗? 我已经搜索并将 jquery.tablesorter.js click 函数更改为 live() 像这样 $headers.live("click",function(e) 但它不起作用。

怎么办我让 jquery 函数在内容加载后工作? 谢谢


更新测试脚本:

<a href="#" id="test" onClick="contentDisp()">Load Content</a>
<div id="result"></div>

<script type="text/javascript"> 
function contentDisp()
{
$.ajax({
url : "test.html",
success : function (data) {
$("#result").html(data);
$("#myTable").tablesorter();
$("#myTable").trigger("update"); 
}
});
}
</script> 

这是我的 test.html。如果我不将其放入加载的内容中,表格排序就会起作用。一旦它被加载到 div 中,排序就不再起作用了。

<table id="myTable" valign="top" class="tablesorter">

    <thead>
      <tr>
        <th class="header">Title 1</th>
        <th class="header">Level 1</th>
      </tr>
    </thead>
    <tbody>
      <tr>
        <td>Example title 1</td>
        <td>Example level 1</td>
      </tr>
      <tr>
        <td>Example title 2</td>
        <td>Example level 2</td>
      </tr>
    </tbody>
  </table>      

I have a link that calls the ajax to load content, and after the content is loaded, my jquery function doesn't work anymore

Here is my HTML

 <a href="#" onclick="javascript:makeRequest('content.html','');">Load Content</a>
    <span id="result">
 <table id="myTable" valign="top" class="tablesorter">
        <thead>
          <tr>
            <th>Title 1</th>
            <th>Level 1</th>
            <th>Topics</th>
            <th>Resources</th>
          </tr>
        </thead>
        <tbody>
          <tr>
            <td>Example title 1</td>
            <td>Example level 1</td>
          </tr>
          <tr>
            <td>Example title 2</td>
            <td>Example level 2</td>
          </tr>
        </tbody>
      </table>
</span>

The table is sorted using jquery table sorter plugin from http://tablesorter.com/docs/
After the ajax content is loaded, another set of table with different data will be displayed. However, the sorting doesn't work anymore.

Here is my ajax script which is use to load the content :

  var http_request = false;
   function makeRequest(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.');
         }
      }
   }

Any idea how I can get the jquery plugins to work after the content is loaded?
I have searched and changed the jquery.tablesorter.js click function into live() like this $headers.live("click",function(e) but it doesn't work as well.

How can I make the jquery functions to work after the content is loaded?
Thank you


Update script for testing :

<a href="#" id="test" onClick="contentDisp()">Load Content</a>
<div id="result"></div>

<script type="text/javascript"> 
function contentDisp()
{
$.ajax({
url : "test.html",
success : function (data) {
$("#result").html(data);
$("#myTable").tablesorter();
$("#myTable").trigger("update"); 
}
});
}
</script> 

This my test.html. The table sorting works if I don't put it in loaded content. Once it's being loaded to div, the sorting doesn't work anymore.

<table id="myTable" valign="top" class="tablesorter">

    <thead>
      <tr>
        <th class="header">Title 1</th>
        <th class="header">Level 1</th>
      </tr>
    </thead>
    <tbody>
      <tr>
        <td>Example title 1</td>
        <td>Example level 1</td>
      </tr>
      <tr>
        <td>Example title 2</td>
        <td>Example level 2</td>
      </tr>
    </tbody>
  </table>      

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

神爱温柔 2024-09-09 08:10:09

插入表数据后需要调用$("#myTable").trigger("update");

请查看 tablesorter 网站上的示例,它也可以帮助您清理加载内容的代码。

如果您需要更多帮助/代码,只需发表评论。 (我认为你最好自己弄清楚,而不是填鸭式的,这就是为什么我不把它包括在这里。)

You need to call $("#myTable").trigger("update"); after the table data is inserted.

Please take a look at this example on the tablesorter website, it may help you clean up your code for the loading of the content as well.

If you need more help / code, just post a comment. (I think it'll be better for you to figure it out instead of spoon-feeding that's why I'm not including it here.)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文