简体   繁体   English

在PHP中使用jQuery Ajax在第一个contenteditable div超过高度后如何添加新的contenteditable div?

[英]How to add new contenteditable div after first contenteditable div exceed the height using jquery ajax in php?

I have one contenteditable div in my webpage but now what I need is, when the max-height of the div will be exceeded then same new div will be automatically created. 我的网页中有一个contenteditable div,但是现在我需要的是,当div的最大高度超过时,会自动创建相同的新div。

Also the div look like previous one. div也像上一个一样。 Not only one div will appear it'ld be 10 to 15 div. 不仅会出现一个div,而是10到15 div。 One more thing when I remove the all content of second div, it will hide. 当我删除第二个div的所有内容时,另一件事将隐藏。 Please anyone one help me. 请任何人帮助我。 From last 15 days I am trying to make this. 从过去的15天开始,我一直在努力做到这一点。

Try this, 尝试这个,

SCRIPT 脚本

$(function(){
    $('div.contenteditable').on('keypress',function(e){
        if(this.offsetHeight>=parseInt($(this).css('max-height'),10)) {
            if(!$(this).next('.contenteditable').length){
                $clone=$(this).clone();
                $clone.html('');
                $clone.insertAfter($(this));
            } else {
                return false;
            }
        }
    });
});

Demo 演示

Updated if you want to bind all contenteditable divs then try this, 如果您想绑定所有contenteditable divs进行更新 ,然后尝试执行此操作,

$(function(){
    $(document).on('keypress','div.contenteditable',function(e){
        // check the offset height with max-height
        if(this.offsetHeight>=parseInt($(this).css('max-height'),10)) {
            // check editable div is inserted after it or not
            if(!$(this).next('.contenteditable').length){
                $clone=$(this).clone();// making clone
                $clone.html('');// empty it
                $clone.insertAfter($(this));// inserting after current div
            } else { // if already exists
                $(this).next('.contenteditable').focus();// focus it
                return false; // and return false for the current
            }
        }
    });
});

Updated demo 更新的演示

To remove the editable divs add this in your code, 要删除editable divs请将其添加到代码中,

var code = e.keyCode ? e.keyCode : e.which;
if (code == 8 || code == 46) {
   if($('div.contenteditable').length >1 && // check length of editable divs
              // replace br's created by break-word property
              $(this).html().replace(/<br*>/,'')=='') { 
     $(this).remove();
     return false;
   }
   return true;
}

Remove div demo 删除div演示

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM