簡體   English   中英

如何從此jQuery函數返回克隆元素

[英]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] = cnewReturnObj[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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM