简体   繁体   English

jquery html 后的新元素

[英]jquery html new element after <span>

I have this HTML:我有这个 HTML:

<div data-value="1606132800">
   <span>23</span>
</div>

And, I want use jQuery to add ap tag after my span like this:而且,我想使用 jQuery 在我的跨度之后添加 ap 标签,如下所示:

<div data-value="1606132800">
   <span>23</span>
   <p>1$</p>
</div>

I have tried:我试过了:

$("div[data-value='"+key+"'] > span").html('<p>'+value.price+' '+currency+'</p>');
$("div[data-value='"+key+"']").children('span').html('<p>'+value.price+' '+currency+'</p>');

I don't want to use append because will apend altot of my p tags and every time before appends a new element i have to remove the p tag to add just 1 tag I am using this at the moment.我不想使用 append 因为会附加我的 p 标签的 altot 并且每次在附加新元素之前我都必须删除 p 标签以添加我目前正在使用的 1 个标签。

 $("div[data-value='"+key+"']").children('p').remove();
 $("div[data-value='"+key+"']").append('<p>'+value.price+' '+currency+'</p>');

The reason for i am trying to get a better solution is that is a bit slow and not very good solution.我试图获得更好的解决方案的原因是它有点慢而且不是很好的解决方案。

Thanks for you help!谢谢你的帮助!

Well you could clone the span the day and than when you can use your html () to put the span and the p tag again.那么你可以克隆当天的跨度,然后你可以使用你的 html () 再次放置跨度和 p 标签。 i will let a exemple with your code:我会用你的代码举一个例子:

  cloneDay[$(this).attr('data-value')] = $(this).children('span').text();

Than after you have your value as a key and your day as a value you can pick from the array the info something like this在您将您的价值作为键并将您的一天作为价值之后,您可以从数组中选择类似这样的信息

  cloneDay[key]

 $("div[data-value='"+key+"']").html('<span>'+cloneDay[key]+'</span><p>'+value.price+' '+currency+'</p>');

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM