dsb238100 2015-10-11 13:56
浏览 80

在.append()之后编辑DIV

My algorithm:

  1. I get the DIV text from php/SQL ($.get( "read.php"...)
  2. APPEND contenteditable div with loaded text by jQuery
  3. check the div value, if there is errors in text I make div red (assign class, $(this).addClass("fill_red");)
  4. on every change of text - check and assign/remove class if needed.

Problem is: with preloaded text - everything is working. But when I append div using JS - check function don't works. I searched the web, maybe on() method helps me.

But what event?

It should be something like onload, onchange..?

(yes, I could make div generated by php and solve the problem, but I dont want full refresh)

Thank you!

part of code:

//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();
} } }); });

  • 写回答

1条回答 默认 最新

  • dqlm80253 2015-10-11 14:09
    关注

    You dont show much code here, but my guess is that you are trying to add class before new data is loaded into dom

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

报告相同问题?

悬赏问题

  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?