当我点击一个链接时,我尝试添加一个字段集。如何使用.append()和appendTo()添加我们想要的元素?
我会认为这个新元素在我的范围之前,因为在这个时候元素被添加了。
<fieldset id="fieldsetOptions">
<legend>Options</legend>
<span>Add an option
<a onclick="addOption()" href="javascript:return false;">
<img src="./images/icons/add.png">
</a>
</span>
<fieldset><!--this is the new fieldset, but not at the good place--></fieldset>
</fieldset>
所以,你明白:良好的结果应该是这个
<fieldset id="fieldsetOptions">
<legend>Options</legend>
<fieldset><!--this is the new fieldset, at the good place--></fieldset>
<span>Add an option
<a onclick="addOption()" href="javascript:return false;">
<img src="./images/icons/add.png">
</a>
</span>
</fieldset>
要添加新的字段集,我这样做:
<script type="text/javascript">
function addOption(){
$("#fieldsetOptions").append("<fieldset></fieldset>");
}
</script>
是否有可能加入其中我们想要 ?
内嵌的JavaScript('的onclick = “... ”')和'HREF =“ JavaScript的:” ......'是完全DIS推荐的,尤其是* *当你已经拥有的东西像jQuery的地方。 – Tomalak
感谢提示 – bahamut100