2012-06-27 42 views
0

我真的很奇怪的行爲。我有這個代碼(我刪除了無關的代碼)。HTML jQuery的功能不工作,但replaceWith確實

$(function() { 
     function get_translations() { 
      $('#translations tr').not(':eq(0)').remove(); 
      var item = { 
       context: "1: context", 
       term: "2: term", 
       translation: "3: translation" 
      }; 

      var tr = $('<tr/>'). 
       append('<td class="context">' + (item.context || '&nbsp;') + '</td>'). 
       append('<td class="original-string">' + item.term + '</td>'). 
       append('<td class="translation translated-string">' + item.translation + 
         '</td>'). 
       append('<td class="delete-col"><button class="'+ 
         'delete translation-delete">X</button></td>'). 
       data('term', item.term || ''). 
       data('translation', item.translation || ''). 
       data('context', item.context || ''). 
       appendTo('#translations'); 
     } 
     get_translations(); 

     $('td.translation').live('click', function() { 
      var $this = $(this); 
      if ($this.find('textarea').length == 0) { 
       var text = $this.empty().parent().data('translation'); 
       //$this.data('content', text).empty(); 
       $this.html('<tex' + 'tarea>' + text + '</text' + 
       'area><button>Save</button><a href="#" class="_cancel">Cancel</a>'); 
       //appendTo($this); 
      } 
     }); 
     $('td.translation button').live('click', function() { 
      var td = $(this).parent(); 
      var tr = td.parent(); 
      console.log(td); 
      td.html('foo'); 
      //td.replaceWith('<td class="translation translated-string">foo</td>'); 
     }); 
}); 

$('td.translation button')處理td.html('foo');不工作,但td.replaceWith('<td class="translation translated-string">foo</td>');一樣。沒有錯誤,它什麼都不做。

我嘗試重新使用該此行爲:

$(function() { 
    $('table').append('<tr><td>:empty</td></tr>'); 
    $('td').live('click', function() { 
     var tr = $(this).parent(); 
     $(this).html('<textarea></textarea><a class="foo" href="#">a:</a>'); 
    }); 
    $('.foo').live('click', function() { 
     var td = $(this).parent(); 
     var tr = td.parent(); 
     td.html(':empty'); 
     return false; 
    }); 
}); 

但上面的代碼工作。

我可以使用replaceWith,但我怎麼知道爲什麼html函數不起作用。任何人都知道爲什麼?

UPDATE:當我添加這個window.td = td;我可以從控制檯調用html,它的工作。

+0

比「不工作」更具體 - 什麼都不發生,你會得到一個Javascript錯誤,你會得到意想不到的結果嗎? –

+0

預期結果是什麼? '.html('foo')'不會創建一個帶有foo類的DOM元素當然... – gdoron

+0

@AonyonyGrist @gdoron它應該做'​​foo'但它不會,沒有任何事情發生,當我做' td.replaceWith('​​foo');'然後它將td更改爲foo。在我的工作代碼中,它是內聯編輯。 – jcubic

回答

0

使用提示從@Tats_innit我把alert('call')$('td.translation').live('click'當我點擊該按鈕被執行的。首先執行按鈕處理程序(它會更改html和警報消息框),然後調用td的處理程序並再次創建textarea。

+0

中也看到我的評論我可能會推薦':)'請使用'.on'而不是'.live',它在最新版本中已棄用。所以我希望現在能夠解決,對吧?即我將停止閱讀您的代碼。 ':P' –

+0

@Tats_innit是的,這是解決的謝謝。 – jcubic

+0

耶,快樂':)'次,也請使用'.on' –

1

演示在這裏看見,http://jsfiddle.net/QknuZ/2/(與td.html()

我希望這回答了你的問題看警告:HTML html()意志取代了元素的內容,而replaceWith()代替實際元素。

http://api.jquery.com/html/

http://api.jquery.com/replaceWith/

replaceWith去除DOM元素,然後再回到會將HTML。

請糾正我,如果我錯了,希望這有助於。

圖片

enter image description here

+0

太棒了,但爲什麼這項工作然後http://jsfiddle.net/27c2V/(這是我想要複製這種行爲的問題中的代碼)。 – jcubic

+0

@jcubic cooleos,我很確定89%的動態html會被添加,比如jsfiddle的文本區域和上面提到的主代碼。讓我看看你的新小提琴中的代碼':)' –

+0

感謝您的提示,我添加了自己的答案,在按鈕處理程序之後執行了td的事件處理程序(由於事件冒泡)。可能replaceWith用DOM元素刪除這個事件。 – jcubic