[英]How can I return the cloned element from this jQuery function
Here's what i have: 这就是我所拥有的:
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;
}
I can't figure out what to return back out to make it chain, like: 我无法弄清楚要返回什么来使它成为链条,如:
$('.example').convertInputType('password').css('background','blue');
I thought i could build an array like newReturnObj[i] = o.replaceWith(c)
or newReturnObj[i] = c
or newReturnObj[i] = jQuery(c)
, but none work. 我以为我可以构建一个像newReturnObj[i] = o.replaceWith(c)
或newReturnObj[i] = c
或newReturnObj[i] = jQuery(c)
这样的数组,但没有一个工作。
Here, this works: 在这里,这工作:
$.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];
});
};
Live demo: http://jsfiddle.net/qTqQr/1/ 现场演示: http : //jsfiddle.net/qTqQr/1/
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.