![](/img/trans.png)
[英]How can I access the attributes of an element cloned via jquery within a javascript function?
[英]How can I return the cloned element from this jQuery function
这就是我所拥有的:
jQuery.fn.convertInputType = function(t){
var e = this;
e.each(function(i){
var o = jQuery(this)
, c = o.clone();
c.attr('type',t);
o.replaceWith(c);
});
return e;
}
我无法弄清楚要返回什么来使它成为链条,如:
$('.example').convertInputType('password').css('background','blue');
我以为我可以构建一个像newReturnObj[i] = o.replaceWith(c)
或newReturnObj[i] = c
或newReturnObj[i] = jQuery(c)
这样的数组,但没有一个工作。
在这里,这工作:
$.fn.convertInputType = function ( type ) {
// we use .map() because we're replacing the set of elements
return this.map( function ( i, elem ) {
var clone = $( this ).clone().attr( 'type', type );
$( this ).replaceWith( clone );
return clone[0];
});
};
现场演示: http : //jsfiddle.net/qTqQr/1/
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.