![](/img/trans.png)
[英]Safari : jQuery functionality not working in ajax loaded content
[英]Ajax loaded content , jquery plugins not working
我有一个调用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/中的 jquery表排序器插件对该表进行排序。加载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>
您需要调用$("#myTable").trigger("update");
在插入表数据之后。
请在tablesorter网站上查看此示例 ,它也可以帮助您清理代码以加载内容。
如果您需要更多帮助/代码,只需发表评论。 (我认为最好弄清楚它而不是用汤匙喂食,这就是为什么我不在这里包括它的原因。)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.