我是jQuery的新手,不知道有没有人能给我一些最佳实践的建议。
我正在寻找附加一个div元素到页面,其中包含大量的is....or,不确定什么是实现此div的最佳方式,如果使用jquery是可取的…
例如,如果我想使用jquery将以下代码附加到页面,那么最好的方法是什么。
<div id="test">
<h1>This is the test</h1>
<p>Hello, just a test</p>
<a href="www.test.com">Click me</a>
<a href="www.test.com">Click me again</a>
</div>
发布于 2009-12-01 18:58:00
如果您想按原样添加HTML,那么只需使用jQuery append函数。例如:
$('body').append('
<div id="test">\
<h1>This is the test</h1>\
<p>Hello, just a test</p>\
<a href="www.test.com">Click me</a>\
<a href="www.test.com">Click me again</a>\
</div>');
根据需要将选择器从body改为其他DOM元素/选择器。
或者,如果文档中已经有ID为"test“的div元素,则可以使用html()函数设置内容,如下所示:
$("#test").html('<h1>This is the test</h1>\
<p>Hello, just a test</p>\
<a href="www.test.com">Click me</a>\
<a href="www.test.com">Click me again</a>');
发布于 2009-12-01 18:35:43
$("#id_of_div").append($("#id_of_div_that_you_wanna_append").html());
发布于 2009-12-01 18:30:39
$("#test").append("YOUR CONTENT GOES HERE");
https://stackoverflow.com/questions/1825306
复制相似问题