日期:2014-05-16 浏览次数:20351 次
<style type='text/css'> input.focus{border:1px solid red; background-color:yellow;} </style> <input type="text" name="testinput" check="num" id="myinput" /> <script type="text/javascript"> var originalInput = $('myinput'); $E.on(input,'focus',function(){$D.addClass(this,'focus');}); $E.on(input,'blur',function(){$D.removeClass(this,'focus');}); var clonedInput = originalInput.cloneNode(true);//deep copy clonedInput.removeAttribute('id');//remove id document.body.appendChild(clonedInput); </script>
cloneNode Method Copies a reference to the object from the document hierarchy. Syntax oClone = object.cloneNode([bCloneChildren]) Parameters bCloneChildren Optional. Boolean that specifies one of the following values: FALSE Cloned objects do not include childNodes. TRUE Cloned objects include childNodes. Return Value Returns a reference to the newly created node. Remarks The cloneNode method copies an object, attributes, and, if specified, the childNodes. When you refer to the ID of a cloned element, a collection is returned. cloneNodedoes not work on an IFRAME directly. You must call cloneNodethrough the all collection.
<script type="text/javascript"> var originalInput = $('myinput'); originalInput.onfocus = function(){$D.addClass(this,'focus');}; originalInput.onblur = function(){$D.removeClass(this,'focus');}; var clonedInput = originalInput.cloneNode(true);//deep copy clonedInput.removeAttribute('id');//remove id document.body.appendChild(clonedInput); </script>
<script type="text/javascript"> var originalInput = $('myinput'); $E.on(input,'focus',function(e){ $D.addClass($E.getTarget(e),'focus');//use $E.getTarget(e) method to get the correct event obj. }); $E.on(input,'blur',function(e){$D.removeClass($E.getTarget(e),'focus');}); var clonedInput = originalInput.cloneNode(true);//deep copy clonedInput.removeAttribute('id');//remove id document.body.appendChild(clonedInput); </script>
jQuery explanation on IE issue: IE copies events bound via attachEvent when using cloneNode. Calling detachEvent on the clone will also remove the events from the orignal. In order to get around this, we use innerHTML. Unfortunately, this means some modifications to attributes in IE that are actually only stored as properties will not