我想在jquery对象的末尾添加一个字符串。事情是这样的:jquery问题:给jquery对象添加东西
var mytable = $("<table><tbody><tr><td>");
mytable.after("Test</td></tr></tbody></table>");
我试着用太
mytable += "Test</td></tr></tbody></table>";
也不起作用。这里
我想在jquery对象的末尾添加一个字符串。事情是这样的:jquery问题:给jquery对象添加东西
var mytable = $("<table><tbody><tr><td>");
mytable.after("Test</td></tr></tbody></table>");
我试着用太
mytable += "Test</td></tr></tbody></table>";
也不起作用。这里
jQuery不能这样工作。当你在处理jQuery中的元素时,它们总是有效的。如果它无法验证它,它将不会添加它。我想你的榜样和jQuery完成了无效的HTML:
var mytable = $("<table><tbody><tr><td>");
// mytable is equal to $("<table><tbody><tr><td></td></tr></tbody></table>");
如果你正在寻找一个新的小区加入到表格中,你可以选择所需的元素(即tr
),然后使用append方法
mytable.find("tr").append("<td>New cell</td>");
当然,你可以添加两个字符串连接在一起,并把它们放在一个jQuery对象,只要他们加入,使有效的HTML
var string1 = "<table><tbody><tr><td>First half and";
string1 = " second half</td></tr></tbody></table>";
mytable = $(string1 + string2);
问题是,
"<table><tbody><tr><td>" //jQuery will try to create full html elements from what you enter
是无效的HTML也不是
"Test</td></tr></tbody></table>" // this will never work, cause this tags are closing only and the table might have been already build when the line before gets executed
jQuery的也许能够创造从没有完全有效的HTML有效元素,但是这并不能在任何情况下工作。 我建议你尝试使用有效的html工作!
JQ除非您提供完整有效的HTML,否则uery无法创建页面元素。我不确定你的确切需求,但你应该在这条线上做点什么:
var mytable = $("<table><tbody><tr><td></td></tr></tbody></table>").find("td").text("Test");
好吧,我认为这有助于我更好地理解jquery。我想我现在知道如何解决这个问题。谢谢你,谢谢大家的帮助。 – huuerequer 2010-12-10 10:20:16