【发布时间】:2011-10-13 21:58:14
【问题描述】:
我正在通过XHR 将另一个页面加载到el。加载的页面上有js,除非页面上加载了所需的dom元素,否则会抛出错误并失败。因为它是 XHR .ready 等。人。不会工作。
我让它在 500 毫秒超时下工作,但这不行;一定有更好的方法。使用超时,dom el 并不总是加载并且页面失败。
页面上有一个硬编码的表格,带有id。页面中的脚本是一个 jquery 插件(数据表),除非加载了表,否则不会初始化。
我考虑过使用一个函数来初始化数据表内容并重复调用 while $('#tableID') 是 null 但不确定这是否正确。
<div id="contactQueue">
<table cellpadding="0" cellspacing="0" border="0" class="display" id="contactsQueueTable">
<thead>
<tr>
<th class="" rowspan="1" colspan="1" style="width: 185px; ">Contact Name</th>
<th class="" rowspan="1" colspan="1" style="width: 148px; ">Bus. Name</th>
<th class="" rowspan="1" colspan="1" style="width: 116px; ">Is Client</th>
<th class="" rowspan="1" colspan="1" style="width: 165px; ">Remove</th>
</tr>
</thead>
<tbody>
</tbody>
</table>
</div>
<?php echo form_open('',array('id'=>'step2form','name'=>'step2form'));?>
<input type="hidden" name="clientID" value="<?php echo $clientID; ?>">
<?php echo form_close();?>
<script type="text/javascript" charset="utf-8">
console.log(jq('#currentContactsTable'))
while(jq('#currentContactsTable').html()==null){
initXHRPage();
console.log(jq('#currentContactsTable')+' not ready');
}
function initXHRPage(){
//init tables stuffs
}
编辑;
问题是别的东西,有点。数据表的脚本正在通过getScript() 加载。元素正在正常加载,但 initDatatables() 在 getScript() 完成加载之前触发,而不是在加载 el 之前触发。
解决方法是在getScript()成功回调中调用initDatatables()函数:
<script type="text/javascript" charset="utf-8">
var jsf = '/js/jquery.dataTables.js';
jq.getScript(jsf, function(data, textStatus){
console.log('Loaded:'+jsf+" From: <?php echo $this->uri->uri_string(); ?>");
initDatatable();
});
</script>
【问题讨论】:
标签: javascript jquery ajax