我想有一個觸發間隔DOM元素被初始化,像這樣一次事件:如何在每次在jQuery中加載元素時調用.ready?
HTML
<div id="container">
<div id="data">data!</div>
</div>
<div id="clickme">Click me!</div>
的Javascript
$(function() {
var i = 0;
$('#clickme').click(function() {
$('#container').html('<div id="data">data '+i+'</div>');
i++;
});
// this event gets called only the first time #data element is ready
// I want it to be called each time #data comes into being
$('#data').ready(function() {
alert('element data ready!');
});
});
我試過了:
// doesn't work
$('#data').live('ready', function() {
alert('element data ready (live)');
});
編輯:我有興趣做,對於由AJAX插入的元素。例如:
$.post('/myaction/', {
param: 'value'
},
function(data) {
$('#someContainer').replaceWith(data);
});
// this function operates on elements returned by the ajax call, so they are not available at this point.
myFunction();
// so I'd better call myFunction() when the ajax data is inserted
});
小提琴這裏:http://jsfiddle.net/nf8CP/
爲什麼你會需要一個事件?爲什麼不在創建元素時調用代碼? –
元素初始化是什麼意思? –
我認爲「就緒」是當window.onload啓動,並document.readyState ==「完整」..並且只發生一次! – 2012-10-12 16:17:43