2012-03-22 237 views
1

感謝您提前審查我的帖子。如何在div中設置p標籤,在td標籤中設置div標籤

我只使用jQuery大約一個星期了,到目前爲止,它非常整齊。看起來我卡住了。我有一張具有以下結構的表格:

例如,我有一張ID爲「mytable」的表格。 具有以下單元格的行:

  • 帶有div標記的td。 div標籤有一個標籤,標識爲「myP1」
  • 帶有div標籤的td。 div標籤有一個p標籤,其ID爲「myP2」

沒有我做的似乎工作?我如何設置「p」標籤內的html?這似乎沒有工作:

//note: the key would match one of the ids in the "p" tags. 
jQuery.each(map, function(key, value) { 
    $('#mytable td').find('p[id=' + key + "]'").html(value); 
    //this did not work either --> $('#' + key).html(value); 
}); 

謝謝你的幫助inadvance!

+0

請粘貼HTML代碼,這樣我們就不必渲染了一切在我們的腦海中。 – joar 2012-03-22 19:10:14

+0

如果關鍵字符合'P' id,代碼應該可以工作...... map有什麼用途? – charlietfl 2012-03-22 19:15:43

+0

'$('#'+ key)'&'find('p [id ='+ key +「]'」)'不起作用,因爲你的id名不僅僅是一個數字。問題是,你的地圖變量對象/數組中的「關鍵」是一個索引號還是它實際上是一個像「myP1」這樣的字符串? – SpYk3HH 2012-03-22 19:38:52

回答

0
// this is jquery method for window.onload = function ... 
$(function() { 
    // this tells jQuery to get the element with the ID "myP1" and fill it's text with what's in() of .text 
    $("#myP1").text('Type a string here and it will be the text') 
}); 

爲你的答案的其餘部分,我想:

$(function(){ 
    $.each(map, function(i, data){ // i is 0 based index, or could be key 
            // in object like var map = { key1: 'value1' } 
     $("#myP"+(i+1)).text(data); 
    ​}); 
}); 

here is a working example with even more fun uses of jQuery, will add comments to it and update it shortly

+0

感謝代碼示例! – sasims 2012-03-23 22:45:12

1

要更新所有p標籤在該表中一個div裏面,你可以做到這一點

$(function(){ 

    $("#mytable div p").html("dynamic content");  

}); 

這是工作樣品

http://jsfiddle.net/2gWHW/3/

如果要上傳只有特定的P,可以使用的ID,像這樣

$("#myP1").html("new content"); 
+0

不幸的是,這些都沒有工作。 – sasims 2012-03-22 20:48:15

+0

@sasims:你檢查了我發佈樣本的鏈接嗎?我想你還有其他一些腳本錯誤。使用螢火蟲查看它在控制檯選項卡中顯示的錯誤 – Shyju 2012-03-22 21:55:22

+0

感謝Shyju的幫助! – sasims 2012-03-23 22:46:05