1

js初心者です。PHPAdminのようなエディタを作りたいです。テーブルをクリックすると、フィールドがテキストエリアに変わります。テキスト領域の外側のどこかをクリックすると、フィールドに戻り、SQL が実行されます。

以下は、私がjQueryで書くと思われるものです。それをさらにコーディングする方法がまったくわかりません。アドバイスをお願いします。

$('#editor #gird_edit').bind({
  click: function() { //When Click
      var content = $(this).text(); // read what is in the filed
      $("#gird_edit").text('<textarea>'+a+'</textarea>'); // This is not work, will only add html code,not change to text-area
  },
  /* ??? */: function() { //Outside click of the text-area 
      var content = $(this).text(); // read what is in the text-area
      $("#gird_edit").text(????);  // change back to the filed 
  }
})

HTML

<div id='editor'>
   <div id='gird_edit'>hallo world</div>
   <div id='gird_edit'>hallo world 2</div>
   <div id='gird_edit'>hallo world 3</div>
</div>

私は評判が 3 つしかないのですが、昨日参加したばかりです... 評判が 15 必要なため、投票できず申し訳ありません。しかし、私はあなたの助けに感謝します!!

4

1 に答える 1

2

要素の外側のクリックを検出したい場合は、ページ全体でクリックを検出し、要素の内側からのクリックをすべて破棄します。言い換えると:

$('body').on('click', : function(e) { //Outside click of the text-area 
    if ($(this).parents().is('#gird_edit')) return false;
    var content = $('textarea').text(); // read what is in the text-area
    $("#gird_edit").text(????);  // change back to the filed 
});

ただし、実際に探しているのは「ぼかし」ハンドラーのようです。これは、誰かがテキストエリア内にいて、そこを離れたときにトリガーされます。クリックハンドラーを作成したのと同じ基本的な方法で、これらのいずれかを作成できます。

$('#gird_edit textarea').bind({
    blur: function() {
        // do the reverse of the click handler
}
于 2012-12-22T00:01:09.037 に答える