简体   繁体   English

将 jQuery 应用于动态插入的 div

[英]Apply jQuery to dynamically inserted divs

I am writing a userscript using jQuery.我正在使用 jQuery 编写用户脚本。 I am trying to change the content of the divs with the class foo so it contains bar .我正在尝试使用foo类更改 div 的内容,以便它包含bar With a static list of elements, I can do it using $(document).ready() :使用静态元素列表,我可以使用$(document).ready()

 // The userscript $(document).ready(function() { $(".foo").text("bar"); });
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <div id="content"> <div class="foo">foo</div> <div class="foo">foo</div> </div>

However, I can't figure out how to replace the text of divs that get inserted dynamically:但是,我无法弄清楚如何替换动态插入的 div 文本:

 // The userscript $(document).ready(function() { $(".foo").text("bar"); }); // A function to dynamically add another div: $("#add").click(function() { $("<div></div>").text("foo").addClass("foo").appendTo("#content"); });
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <div id="content"> <div class="foo">Div 1</div> <div class="foo">Div 2</div> </div> <button id="add" type="button">Add</button> // Click to

Using $(document).ready() only works on the statically inserted divs, as those are the only ones there when the document is loaded.使用$(document).ready()仅适用于静态插入的 div,因为它们是加载文档时唯一存在的 div。

How can I make the divs that are dynamically added to #content get their content changed to bar ?如何让动态添加到#content的 div 将其内容更改为bar

Note sure why you don't just change the $("<div></div>").text("foo") to $("<div></div>").text("bar") ;请注意为什么不只是将$("<div></div>").text("foo")更改$("<div></div>").text("bar")

But the code in your $(document).ready() only runs once.但是$(document).ready()的代码只运行一次。

so do something like this.所以做这样的事情。

// The userscript
$(document).ready(function() {
  footext();
});

// A function to dynamically add another div:
$("#add").click(function() {
  $("<div></div>").text("foo").addClass("foo").appendTo("#content");
  footext();
});

function footext() {
  $(".foo").text("bar");
}

Demo演示

 // The userscript $(document).ready(function() { footext(); }); // A function to dynamically add another div: $("#add").click(function() { $("<div></div>").text("foo").addClass("foo").appendTo("#content"); footext(); }); function footext() { $(".foo").text("bar"); }
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <div id="content"> <div class="foo">Div 1</div> <div class="foo">Div 2</div> </div> <button id="add" type="button">Add</button> // Click to

You can also use DOMNodeInserted你也可以使用DOMNodeInserted

$('body').on('DOMNodeInserted', '.foo', function () {
      $(this).text("bar");
});

Demo演示

 // The userscript $(document).ready(function() { $(".foo").text("bar"); }); // A function to dynamically add another div: $("#add").click(function() { $("<div></div>").text("foo").addClass("foo").appendTo("#content"); }); $('body').on('DOMNodeInserted', '.foo', function () { $(this).text("bar"); });
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <div id="content"> <div class="foo">Div 1</div> <div class="foo">Div 2</div> </div> <button id="add" type="button">Add</button> // Click to

Add this添加这个

`$(".foo").text("bar");` 

inside the button click function.在按钮点击功能里面。

 // The userscript $(document).ready(function() { $(".foo").text("bar"); }); // A function to dynamically add another div: $("#add").click(function() { $("<div></div>").text("foo").addClass("foo").appendTo("#content"); $(".foo").text("bar"); });
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <div id="content"> <div class="foo">Div 1</div> <div class="foo">Div 2</div> </div> <button id="add" type="button">Add</button> // Click to

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

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