我有一个填充了 AJAXonchange
请求结果的表。一切正常,但我还有一个包含在 window.load 上的 JS 脚本,它基于mouseover
、mouseout
、onclick
等更改表。但是,由于在 AJAX 请求之前不会填充表,因此表 JS 功能不起作用。有没有办法用 AJAX 请求填充表格,而不是做 window.load,在 AJAX 请求完成期间或之后加载 JS 脚本?下面是我的代码:
<link href="<?php echo $this->config->item('base_url'); ?>/css/table_design.css"
rel="stylesheet" type="text/css" media="screen" />
// This is the script that is included, and inside the script
//the main function is started by window.load
<script type="text/javascript"
src="<?php echo $this->config->item('base_url'); ?>/js/table_design.js"></script>
<br />
<script type="text/javascript">
function showUser(str) {
if (window.XMLHttpRequest) {// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}
else {// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function() {
if (xmlhttp.readyState==4 && xmlhttp.status==200) {
document.getElementById("tars").innerHTML=xmlhttp.responseText;
}
}
xmlhttp.open("GET","jqueryDB/catSelector?cat="+str,true);
xmlhttp.send();
}
</script>
<span id="tars"></span>
这是我通过 AJAX 请求的 PHP 页面:
public function catSelector()
{
$catID = $_GET['cat'];
$query = $this->db->query('SELECT * FROM upload WHERE category = '.$catID);
$num_rows = $query->num_rows();
$val = $query->result_array();
echo '<table cellspacing="0" cellpadding="0">
<tr>
<th width="50%">File Name:</th>
<th width="30%">File Size:</th>
<th>Download Link:</th>
</tr>';
$c = true;
foreach($val as $files){
if ($files['size'] >= 1000000000) {
$files['size'] = round( ($files['size'] / 1000000000) , 2).' GB';
}
if ($files['size'] >= 1000000) {
$files['size'] = round( ($files['size'] / 1000000) , 2).' MB';
}else{
$files['size'] = round( ($files['size'] / 1000) , 2).' KB';
}
echo '<tr'.(($c = !$c)?' class="odd"':'').">
<td>$files[name]</td>
<td>$files[size]</td>
<td><a href='$files[location]' >Download</a></td>
</tr>";
}
echo '</table>';
}