替换为之后的Dom就绪事件

Dom-ready event after replaceWith

本文关键字:就绪 事件 Dom 之后 替换      更新时间:2023-09-26
  1. 我通过用.replaceWith((JQuery方法替换一些divs,将textareas添加到html页面中。

  2. 我想让我的textareas自动作为那里的用户类型。

问题是,我使用的autogrow库只适用于dom就绪,因此我无法将我的autogrove库应用于我的textareas,因为它们是动态生成的。

在我的divstextareas替换后,有什么事件可以用来应用我的自动生成库吗?

您必须使用事件处理程序来捕获对文本区域的更改(即使它们是动态生成的(,例如

jQuery(function($){
    // dom ready
    $("body").on("input propertychange","textarea",function(event) {
        // here goes your autogrow code
    });
});

在JSFiddle 中查看动作

或此处

jQuery(function($) {
  // dom ready
  $("body").on("input propertychange", "textarea", function(event) {
    // here goes your autogrow code
    alert("changed");
  });
  $("#addTextArea").click(function(ev) {
    $("#textareas").append("<br/><textarea></textarea>");
  });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<div id="textareas">
  <textarea></textarea>
</div>
<button id="addTextArea">Add textarea</button>

您可以使用JQuery change((。

这样,您就可以在用户键入内容时调用您的autogrow库。

$("textarea").change(function(){
    //Your code here
})

您可以使用$.holdReady(),然后在所有div都更改为文本区域后释放ready函数。

//code to prevent .ready() from firing
$.holdReady( true );
//code that releases (essentially triggers) .ready()
//use this in the callback function once your conversion 
//from DIVs to textareas completes
$.holdReady( false );



JSFiddle演示

jQuery.holdReady((引用