Javascript 如何从这个jQuery函数返回克隆的元素
以下是我所拥有的:Javascript 如何从这个jQuery函数返回克隆的元素,javascript,jquery,plugins,Javascript,Jquery,Plugins,以下是我所拥有的: 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').convertInputT
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];
});
};
现场演示:
jQuery(“#通过电子邮件发送邀请”).find(“.email.partial”).css('background','red').convertInputType('password').css('background','green')代码>输入永远不会变成绿色:(顺便说一句,方法名称很长…setType如何?setType只是让我担心它太模糊了,而且“setting type”与“type setting”的意思是设置字体样式。我本来打算使用它,但在大约150名工程师的庞大代码库中对此感到担心。