我使用document.createElement来创建一个具有以下类的div:

var newDiv = document.createElement("div");
newDiv.className = 'note';


但是我不知道如何使div具有.note的html元素,并且每次我触发该块时,它都会加倍,但是注释的数量是多少!因此,如果有4个注释,它将创建4个注释,从而产生8个注释:((。note是我要复制的div)

这是完整的代码:

HTML:

<div class="note">
  <input id="subject" value="SUBJECT"></input>
  <div class="time"></div>
  <hr>
  <ul>
      <li>
          <input type="text" value="TYPE HERE"></input>
      </li>
  </ul>
</div>


CSS:

.note {
    padding:10px;
    background-color:#FFFF19;
    height:400px;
    width:400px;
    border-radius:1px;
    display:inline-block;
    margin:5px;
}
input {
    background-color:#ffff19;
}
#subject {
    background-color:#ffff19;
    border:none;
    width:73px;
}
.time{
    height:25px;
    width:300px;
    float:right;
}

.note hr{
    width;350px;
}


JS:

$('#wrp').click(function newNote() {
    var newDiv = document.createElement("div");
    newDiv.className = 'note'
    var currentDiv = document.getElementById("wrp");
    $(newDiv).insertBefore('.note');
}

最佳答案

或者,您可以简单地clone()要复制的.note元素。

$('#wrp').click(function() {
    var note = $('.note').first(); // assuming the first .note to clone
    note.clone().insertBefore(note);
});

关于jquery - jQuery使用元素创建Div,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/30032427/

10-14 17:44
查看更多