[英]javascript copy div change innerHTML id
I need to copy content from one div to another one with changing button's id (increment them) 我需要使用更改按钮的ID(将它们递增)将内容从一个div复制到另一个div
Sample: 样品:
<script type="text/javascript">
function add(){
document.getElementById('two').innerHTML = document.getElementById('one').innerHTML;
}
</script>
<div id="one">
<button id="button_1" >Button_1</button>
</div>
<div id="two"></div>
<button onclick="add();">Add</button>
This of course can't work properly. 这当然不能正常工作。
Result should be following: 结果应为:
<div id="two">
<button id="button_2" >Button_2</button>
</div>
Any simple way how to do this ? 任何简单的方法怎么做到的?
If you want copy the button onclick of a button it will work for you i guess.. 如果您想复制按钮的onclick按钮,它将为您工作。
document.getElementById('button').onclick = duplicate; var i = 0; var original = document.getElementById('one'); function duplicate() { var clone = original.cloneNode(true); // "deep" clone clone.id = "one" + ++i; // there can only be one element with an ID original.parentNode.appendChild(clone); }
<div id="one"> <button id="button_1" >Button_1</button> </div> <button id="button" style="color:red">Add</button>
Are you looking for something like this. 您在寻找这样的东西吗? With a good mastery of jQuery traversal you may not even need to give each button an id.
精通jQuery遍历,您甚至不需要为每个按钮指定一个ID。 May be a common class may serve you well.
可能是一个普通的班级可能会为您服务。
$(function() { var last = $('.container'); var curr = last; $('#add').on('click', function() { last = curr.clone(); last.find('button').attr('id', function() { return 'button_' + ( $('div.container').length + 1 ); }) .html(function() { return 'Button_' + ( $('div.container').length + 1 ); }).end() .insertAfter( curr ); curr = last; }); });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class="container"> <button id="button_1" >Button_1</button> </div> <button id="add">Add</button>
Using JQuery: 使用JQuery:
function add( fromId, toId ){
content = $('#'+fromId).clone();
button = content.find('button');
if( button.length == 1 ){
buttonId = button.attr('id');
number = buttonId.match(/\d+/g);
number = parseInt(number[0]) + 1;
button.attr('id','button_' + number);
button.html('Button_' + number);
}
$('#'+toId).html(content.html());
}
Just call 刚打电话
add('one','two');
I've made small change in 'id' of the wrapping div. 我在包装div的'id'中做了小小的改动。
<div id="1" class="button">
<button id="button_1" >Button_1</button>
</div>
<button onclick="add();">Add</button>
<script type="text/javascript">
function add(){
var count = document.querySelectorAll('.button').length;
var newCount = count+1;
var elDiv = document.createElement('div');
elDiv.setAttribute("id", newCount);
elDiv.setAttribute("class", "button");
elDiv.innerHTML = '<button id="button_"+newCount+">Button_'+newCount+"</button>";
document.getElementById(count).appendChild(elDiv);
}
</script>
However it can be done in more simpler way using jQuery. 但是,可以使用jQuery以更简单的方式完成此操作。 Hope this helps.
希望这可以帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.