使用jQuery将此元素及其子元素添加到DOM的最佳方法是什么? 假设我需要追加的div有一个id包装器。
<div id="menu">
<a href="#" id="link1" data-speed="600" data-easing="easeOutBack">About</a>
<a href="#" id="link2" data-speed="600" data-easing="easeOutBack">Updates</a>
<a href="#" id="link3" data-speed="600" data-easing="easeOutBack">Producer</a>
<a href="#" id="link4" data-speed="1000" data-easing="easeInExpo">Performer</a>
<a href="#" id="link5" data-speed="600" data-easing="easeOutBack">Speaker</a>
<a href="#" id="link6" data-speed="1000" data-easing="easeInExpo">Endorser</a>
<a href="#" id="link7" data-speed="1000" data-easing="easeInExpo">Mentor</a>
<a href="#" id="link9" data-speed="600" data-easing="easeOutBack">Press</a>
<a href="#" id="link10" data-speed="600" data-easing="easeOutBack">Contact</a>
</div>
有人愿意帮忙吗?
谢谢,
的Mirko
答案 0 :(得分:1)
$('<div id="menu">...</div>').appendTo('#wrapper');
请确保第一部分中的HTML没有换行符。
答案 1 :(得分:1)
$('<div id="menu">
<a href="#" id="link1" data-speed="600" data-easing="easeOutBack">About</a>
<a href="#" id="link2" data-speed="600" data-easing="easeOutBack">Updates</a>
<a href="#" id="link3" data-speed="600" data-easing="easeOutBack">Producer</a>
<a href="#" id="link4" data-speed="1000" data-easing="easeInExpo">Performer</a>
<a href="#" id="link5" data-speed="600" data-easing="easeOutBack">Speaker</a>
<a href="#" id="link6" data-speed="1000" data-easing="easeInExpo">Endorser</a>
<a href="#" id="link7" data-speed="1000" data-easing="easeInExpo">Mentor</a>
<a href="#" id="link9" data-speed="600" data-easing="easeOutBack">Press</a>
<a href="#" id="link10" data-speed="600" data-easing="easeOutBack">Contact</a>
</div>').appendTo('#wrapper');
答案 2 :(得分:1)
好吧,你可以使用soem方法做追加,但它取决于你想要添加这些元素的位置,看看这个:
.append()
:http://api.jquery.com/append/
插入元素的末尾
.prepend()
:http://api.jquery.com/prepend/
插入元素的开头
我希望它有所帮助
答案 3 :(得分:0)
试试这个
$('#menu').append('#yourIdToInclude')
答案 4 :(得分:0)
$('#menu').appendTo('#elementinwhichtoappend')
答案 5 :(得分:0)
假设目标div的标识为target
,并且您在名为html
的字符串中包含html
$("#target").append(html)