jQuery 单击jquery中的按钮时如何添加div?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6678044/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 21:13:28  来源:igfitidea点击:

How to add a div when clicking a button in jquery?

jquery

提问by Ravichandran Jothi

I need to add the following div structure when i click button at every time? I need to generate the following whole div structure at every time of clicking the button using jquery.

每次单击按钮时都需要添加以下 div 结构吗?我需要在每次使用 jquery 单击按钮时生成以下整个 div 结构。

<div class="module_holder">
<div class="module_item">
<img src="images/i-5.png" alt="Sweep Stakes"><br>sendSMS
</div>
</div>

help me.

帮我。

Thanks Ravi

谢谢拉维

回答by rtpHarry

You need a container div that you can insert it into:

您需要一个可以将其插入的容器 div:

<div id="container"></div>

Then you can use the append() method of jquery:

然后就可以使用jquery的append()方法了:

$("#somebutton").click(function () {
  $("#container").append('<div class="module_holder"><div class="module_item"><img src="images/i-5.png" alt="Sweep Stakes"><br>sendSMS</div></div>');
});

回答by James

Try this out http://jsfiddle.net/4dEsJ/1/I'm sure there are better ways of creating the element, but if the structure and content remains the same, this works!

试试这个http://jsfiddle.net/4dEsJ/1/我相信有更好的方法来创建元素,但如果结构和内容保持不变,这行得通!

回答by BenM

How about this:

这个怎么样:

$('#button_id').click(function() {

    var structure = $('<div class="module_holder"><div class="module_item"><img src="images/i-5.png" alt="Sweep Stakes" /><br />sendSMS</div></div>');
   $('#whatever').append(structure); 

});