2012-03-22 261 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