在 .append() 之后编辑 DIV


Edit DIVs after .append()

我的算法:

  1. 我从 php/SQL 获得 DIV 文本 ( $.get( "read.php"... (
  2. APPEND内容可编辑的div,带有jQuery加载的文本
  3. 检查div值,如果文本中有错误,我div红色(分配类,$(this).addClass("fill_red");(
  4. 每次更改文本时 - 如果需要,请检查并分配/删除类。

问题是:使用预加载的文本 - 一切正常。但是当我使用 JS 附加div 时 - 检查功能不起作用。我在网上搜索,也许on()方法对我有帮助。

但是什么事件?

它应该是像onloadonchange..?

(是的,我可以制作由 php 生成的div并解决问题,但我不想完全刷新(

谢谢!

部分代码:

//texts load $(function() { $.get( "read.php", function( data ) { var ionka = data.split(' '); ionka.forEach(function(item, i, arr) { var app_text = "<div id='"segm" + i + "'" contenteditable role='"textbox'">" + item + "</div>"; $("#textarea").append(app_text); }); }); //checks var intRegex = new RegExp('^[0-9/']{5}$'); $('#textarea div').each(function(i,elem) { if(!intRegex.test($(this).text())) { $(this).addClass("fill_red"); }else{ $(this).removeClass(); } }); // edit on change. Blur because of contenteditable var segm_array = []; $('#textarea div').each(function(i,elem) { $(this).blur(function() { if (segm_array[i]!=$(this).text()){ segm_array[i] = $(this).text(); if(!intRegex.test(segm_array[i])) { $(this).addClass("fill_red"); }else{ $(this).removeClass();
} } }); });

你在这里没有显示太多代码,但我的猜测是你正在尝试在新数据加载到 dom 之前添加类

$.get( "read.php" ).done(function( data ) {
    // addClass etc
});