2012-04-25 78 views
0

我創建了一個標籤,並添加一些文字在它jQuery的。點擊功能無法正常工作

$("#tabcontent").append('<p id="c'+count+'"><table> <tr>Tab Content '+count+'<br><span id="addColumns" style="cursor:pointer;">Add Columns</span></p>'); 

有一個id addColumns我寫一個函數爲這個ID

$('#addColumns').click(function(){ $("#tabcontent").html(''); });

當我點擊Add Columns什麼都沒發生......

+1

你能不能把你的HTML/JS上http://jsfiddle.net – gideon 2012-04-25 05:59:16

+1

1.我使用更新面板烏爾.. 2.可能是烏爾addcolumn認爲是服務器控件... 按@gideon如果你把你的代碼很容易解決... – 2012-04-25 06:00:28

+3

它在JSFiddle中工作。看到這個鏈接: http://jsfiddle.net/sfWxJ/ – Chinmaya003 2012-04-25 06:02:40

回答

1

當頁面加載時,你的表不在DOM中,所以這是未來的元素,這就是爲什麼它是n不工作。你必須使用.live()

$('#addColumns').live('click', function(){ $("#tabcontent").html(''); }); 

或按jQuery的1.7

使用.on

$(document).on('click','#addColumns', function(){ 
     $("#tabcontent").html(''); 
}); 
1

我們剛修好你明白我的HTML結構,然後再試一次。

<p id="c'+count+'"> 
    <table> 
     <tr>Tab Content '+count+' 
      <br> 
      <span id="addColumns" style="cursor:pointer;">Add Columns</span> 
</p> 

此外 - 你真的想清除你點擊跨度的元素?

編輯: 這是你的html代碼,而不是你應該使用的。在你的行內試試td-tag並嘗試關閉你的標籤。