2014-01-15 87 views
0

工作,我嘗試動態頁面上添加文本區域(它的工作原理),然後我嘗試在模糊刪除textarea的()事件,但它不工作模糊()函數不會對textarea的

$(function() { 
$('.edit').one('click', function() { 
    var id = $(this).attr('id'); 
    var comment$ = $(this).parents('li').prev('li.comment-post'); 
    var text = comment$.text(); 
    comment$.html('<textarea name="comment-edit" class="comment-edit">' + text + '</textarea><br />'); 
    $('.comment-edit').focus(); 
}); 

$('.comment-edit').blur(function(){ 
    alert('text'); 
    var text = $(this).val(); 
    alert(text); 
    /*var comment$ = $(this).parents('li.comment-post'); 
    $(this).remove(); 
    comment$.text('text');*/ 
}); 
}); 

http://jsfiddle.net/Wx85E/35/

怎麼了?

回答

1

您應該使用:

$('.comment-edit').live('blur',function(){ 
    alert('text'); 
    var text = $(this).val(); 
    alert(text); 
    /*var comment$ = $(this).parents('li.comment-post'); 
    $(this).remove(); 
    comment$.text('text');*/ 
}); 

我已經住使用,因爲你已經在撥弄增加1.8.3 jQuery庫。

您需要使用的,如果你正在使用的庫1.9,並進一步:

$(document).on('blur', '.comment-edit',function(){ 
//code 
} 

Working Demo

+1

你不應該住用,其棄用,請使用功能istead http://api.jquery.com/on/ – Esko

+0

上使用' '事件代替 – Era

+0

你能否看到OP已經添加了jq庫1.8.3。 Live支持 –

0

因爲,你綁定textarea的活:

$(document).on('blur', '.comment-edit',function(){ 
     alert('text'); 
     var text = $(this).val(); 
     alert(text); 
    }); 
+0

不,'on()'不適用於jquery1.8.3,'on()'只能在1.9開始工作。我得到上面的解釋;) –

0

你是試圖在創建文本區域之前將模糊事件綁定到文本區域。試試這個叉:

http://goo.gl/fU0DVn

2
$(function() { 
    $('.edit').on('click', function() { 
     var id = $(this).attr('id'); 
     var comment$ = $(this).parents('li').prev('li.comment-post'); 
     var text = comment$.text(); 
     if($('.comment-edit').length){ 
      $('.comment-edit').show(); 
     } 
     else{ 
     comment$.html('<textarea name="comment-edit" class="comment-edit">' + text + '</textarea><br />'); 
     } 
     $('.comment-edit').blur(function(){ 

      var text = $(this).val(); 
      comment$.text(text); 
      /*var comment$ = $(this).parents('li.comment-post');*/ 
      $('.comment-edit').hide();/* 
      comment$.text('text');*/ 
     }); 
     $('.comment-edit').focus(); 
    }); 
}); 

Fiddle reference

+0

非常感謝這個答案! –

+0

歡迎您:)玩得開心。 – gokul