Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/79.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何从这个jQuery函数返回克隆的元素_Javascript_Jquery_Plugins - Fatal编程技术网

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名工程师的庞大代码库中对此感到担心。