Javascript 如何检测表中动态生成的行
我有一个表,它的行是动态创建的。Javascript 如何检测表中动态生成的行,javascript,jquery,html,Javascript,Jquery,Html,我有一个表,它的行是动态创建的。 我想在生成tr或td时提示消息[alert] 一开始会是这样 <table class="tab"></table> 看看DOM事件 “DomainNodeInserted-当一个节点被添加为另一个节点的子节点时激发” 如果做不到这一点,您可以在超时的情况下轮询DOM,并向已经处理的所有内容添加一个类 setatimeout... function timeouthandler ( var newitems = $.(".tab
我想在生成tr或td时提示消息[alert] 一开始会是这样
<table class="tab"></table>
看看DOM事件 “DomainNodeInserted-当一个节点被添加为另一个节点的子节点时激发”
如果做不到这一点,您可以在超时的情况下轮询DOM,并向已经处理的所有内容添加一个类
setatimeout...
function timeouthandler (
var newitems = $.(".tab td").not('.processed');
if newitems {
alert('new stuff!');
newitems.addClass('processed')
}
setanothertimeout...
)
这是我脑子里想不出来的,需要做些工作。请随意使用实际有效的内容编辑此答案;-) 您可以使用setinterval并编写一个函数,以便在更改表的html时使用。然后找到附加的td并提醒用户消息
var previous_tr_html='';
setInterval(function() { $checkError(); }, 1000);
function checkError(){
var current_tr_html=$('.row').html();
if(previous_tr_html!=current_tr_html){
//new td added
newerrortds=$('.row').find('td:not(.displayed)');
$(newerrortds).each(function(){
alert($(this).html();)
//add already displayed error class to current td
$(this).addClass('displayed');
});
//change previous html value to current
previous_tr_html=current_tr_html;
}
}
基于此讨论,我建议您一个解决方案:
不幸的是,我不是jquery专家,所以请检查我的代码并进行实验。如果您需要更多帮助,请告诉我。通过事件回调扩展上载程序脚本以报告此情况-这对我来说是最好的方法。我认为使用jQuery可以简化此问题的解决方案 使用此选项可以在一条语句中指定事件和选择器:
$(document).on( "DOMNodeInserted", "tr.row", function() {
alert( "New ROW ADDED" );
});
这里有一个发布您的javascript Too什么是“动态”创建这些行?您如何使用javascript在表中添加行?另一个称为Primefaces的框架,我无法控制它。所以我想,一旦创建了行,我就会处理它。告诉让你这么做的人,这是个坏主意,而且很脆弱,很难维护,也很难排除故障。
var previous_tr_html='';
setInterval(function() { $checkError(); }, 1000);
function checkError(){
var current_tr_html=$('.row').html();
if(previous_tr_html!=current_tr_html){
//new td added
newerrortds=$('.row').find('td:not(.displayed)');
$(newerrortds).each(function(){
alert($(this).html();)
//add already displayed error class to current td
$(this).addClass('displayed');
});
//change previous html value to current
previous_tr_html=current_tr_html;
}
}
$(document).ready(function(){
$(document).bind('DOMSubtreeModified',function(){
$('.template-upload.ui-state-error').each(function( index ){
alert( "The file '"+$(this).find('.name').html()+" is not valid" );
$(this).find('.ui-icon-cancel').click();
});
})
});
$(document).on( "DOMNodeInserted", "tr.row", function() {
alert( "New ROW ADDED" );
});