使用jQuery将HTML标记附加到Class [英] Append HTML tag to Class with jQuery
问题描述
我要添加< ul>< / ul>
标记,而不是类< div class ='box' < / div>
我以这种方式使用appendTo()
$('< ul>')。appendTo('。box');
但我得到:
< div class =box>< li>您未输入您的名字< / li>< ul>< / ul> / div>
> < div class =box>< ul>< li>您未输入您的名字< / li>< / ul>< / div>
如果 li
c $ c> div (这将是无效的html):
$ ').wrapAll('< ul>< / ul>');
应该工作。
code> li 在 ul
后面添加:
$('< li />').text('whatever').appendTo('.box ul');
事实上,您使用 append()
建议 li
已经存在,这似乎有点奇怪。
API参考: p>
-
wrapAll()
, -
appendTo()
,解释为什么append插入ul
code> li ,这似乎让您大吃一惊。
I want to add an <ul></ul>
tag instead a class <div class='box'></div>
I use appendTo() in this way
$('<ul>').appendTo('.box');
But I get:
<div class="box"><li>You did not enter your first name</li><ul></ul></div>
And of couse should i get:
<div class="box"><ul><li>You did not enter your first name</li></ul></div>
If the li
is already in the div
(which would be invalid html):
$('.box li').wrapAll('<ul></ul>');
Should work.
If the li
is added after the ul
is appended:
$('<li />').text('whatever').appendTo('.box ul');
The fact that you're using append()
suggests that the li
is already present though, which seems a little odd.
API references:
wrapAll()
,appendTo()
, explaining why append inserts theul
after theli
, which seemed to surprise you.
这篇关于使用jQuery将HTML标记附加到Class的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!