使用jQuery将HTML标记附加到Class [英] Append HTML tag to Class with jQuery

查看:122
本文介绍了使用jQuery将HTML标记附加到Class的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要添加< 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>


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 the ul after the li, which seemed to surprise you.

这篇关于使用jQuery将HTML标记附加到Class的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆