中的元素的侦听器我想复制一个div及其所有内容,以生成一个克隆的div(例如,添加行的发票dinamically)。我需要克隆div的内容,包括html控件,DIV中元素的动态ID和监听器!要生成克隆的div与html控件,动态ID和DIV
有没有简单的方法来做到这一点?谢谢!
代码工作,但它不是克隆整个DIV也克隆了文本框的ID可以有一个人帮助我。
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
<head>
<style>
body
{
background: none !important;
}
</style>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title> Dynamically create input fields- jQuery </title>
<link href="theme/css/ui/ui.base.css" rel="stylesheet" media="all" />
<link href="theme/css/themes/black_rose/ui.css" rel="stylesheet" title="style" media="all" />
<!--[if IE 6]>
<link href="theme/css/ie6.css" rel="stylesheet" media="all" />
<script src="theme/js/pngfix.js"></script>
<script>
/* Fix IE6 Transparent PNG */
DD_belatedPNG.fix('.logo, ul#dashboard-buttons li a, .response-msg, #search-bar input');
</script>
<![endif]-->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.min.js">
</script>
<script type="text/javascript">
$(function() {
var i = 1;
$('#addNew').live('click', function() {
var getParentId = $(this).closest('fieldset').attr('id');
var getParentId1 = $('#'+getParentId);
i = $('#'+getParentId+' p input').size();
if(i > 6) {
alert("Limitation upto 5");
return false;
}
$('<p><input type="text" id="p_new" name="p_new_' + i +'" value="" placeholder="I am New" /><a href="#" id="remNew">Remove</a> </p>').appendTo(getParentId1);
i++;
return false;
});
$('#remNew').live('click', function() {
if(i > 2) {
$(this).parents('p').remove();
i--;
}
return false;
});
// Duplicate Query Block
var addQueryBlock = $('#queryblock');
var j = $('#queryblockfld').size() + 1;
$('#addNewQueryBlock').live('click', function() {
if(j > 6) {
alert("Limitation upto 5");
return false;
}
$('<fieldset id="queryblock' + j +'" name="queryblock' + j +'"><legend>Query Block' + j + '</legend><div style="float:right;" id="addqueryblock"><a style="text-decoration: none;" href="#" id="remNewFieldset"> – </a><a style="text-decoration: none;" href="#" id="addNewQueryBlock">+</a></div><div id="addinput"><p><input type="text" id="p_new1" name="p_new" value="" placeholder="Input Value" /><a href="#" id="addNew">Add</a><input type="text" id="input2" name="input2" value="" placeholder="input2" /></p></div></fieldset>').appendTo(addQueryBlock);
j++;
return false;
});
$('#remNewFieldset').live('click', function() {
if(j > 2) {
$(this).parents('fieldset').remove();
j--;
}
return false;
});
});
</script>
</head>
<body>
<h2>
Dynamically Add Another Input Box
</h2>
<div id="queryblock">
<fieldset id="queryblockfld" name="queryblockfld">
<legend>
Query Block
</legend>
<div style="float:right;" id="addqueryblock">
<a style="text-decoration: none;" href="#" id="addNewQueryBlock">+</a></div>
<div id="addinput">
<p>
<input type="text" id="p_new3" name="p_new3" value="" placeholder="Input Value" />
<a href="#" id="addNew"> Add </a> Wlcome to my world
<input type="text" id="input2" name="input2" value="" placeholder="input2" />
</p>
</div>
</fieldset>
</div>
</body>
</html>
简单地使用jQuery'.clone()'方法:) – GEMI
但是如何在DIV内输入字段也将克隆到新名称kinldy检查下面的URL我也试过http://jsfiddle.net/ kXmpY /但一个但粘性。 – TomPHP
那么如果你想拥有动态ID和监听器,那么你的问题是不正确的。你不想克隆一个元素:)你想要的是能够创建具有特定参数的HTML元素。我认为你需要制造一个工厂...... :) – GEMI