jQuery 如何在jquery中附加一个元素作为第一个子元素
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4827084/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
how to append an element as a first child in jquery
提问by Vivek
lets say i have html something like this..
可以说我有这样的 html ..
<ul id="leftNavigation">
<table id="leftNavTable"><tr><td>blablabla</td></tr><table>
<li>1</li>
<li>2</li>
</ul>
what i want to do is when i click on any li, then that table should get hide and another table should get appended in place of that table.lets say that table is
我想要做的是当我单击任何 li 时,该表应该隐藏,另一个表应该被附加到该表的位置。让我们说该表是
<table id="Selected"><tr><td>blablabla</td></tr><table>
how to do it in jQuery.
如何在 jQuery 中做到这一点。
i tried in this way but its adding as a last child of ul.
我以这种方式尝试过,但它添加为 ul 的最后一个孩子。
$('#leftNavigation').append('<table id="Selected"><tr><td>blablabla</td></tr><table>')
Thanks in advance!!!!
提前致谢!!!!
回答by Robert Koritnik
Use .prepend()
or .prependTo()
instead.
使用.prepend()
或.prependTo()
代替。
Check jQuery documentation.
检查jQuery 文档。
Additional notes
补充说明
But based on what you're trying to accomplish you should maybe rather use DOM replacement functionality.
但是根据您要完成的工作,您可能应该使用DOM 替换功能。
Beware
谨防
Adding anything else than LI
inside an UL
or OL
is invalid HTML.
LI
在UL
或 中添加任何其他内容OL
都是无效的 HTML。
I suggest you add an additional LI
, give it some ID
or CSS class
and then manipulate its content.
我建议你添加一个额外的LI
,给它一些ID
或 CSS class
,然后操作它的内容。