简体   繁体   English

jQuery.html,添加div元素

[英]jQuery, .html, add a div element

I'm trying to add a div with some details of a discount coupon so the user can confirm it before I add it to the shopping cart. 我正在尝试添加带有优惠券详细信息的div,以便用户在将其添加到购物车之前可以对其进行确认。

The code to the button etc... is this, and it works if I place on the website: 按钮等的代码是这样,如果我放置在网站上,它将起作用:

<div class="simpleCart_shelfItem"><h1 class="item_name">Discount of 30%!</h1><a href="javascript:;" class="item_add">Confirm Coupon!</a><span class="item_price" >0.00€</span></div>

Then, I tried to dynamically add the code (when a php post confirms that the coupon code is OK) using jQuery, but by some strange reason it doesn't work. 然后,我尝试使用jQuery动态添加代码(当php发布确认优惠券代码正确时),但是由于某种奇怪的原因,它不起作用。

I managed to write this: 我设法这样写:

$('div.cupao_confirm').html('<div class="simpleCart_shelfItem"><h1 class="item_name">Discount of 30%!</h1><a href="javascript:;" class="item_add">Confirm Coupon!</a><span class="item_price" >0.00€</span></div>');

Without jQuery the code add the "0.00€" product to the shopping cart, so I can later send the equivalent money back to the buyer. 如果没有jQuery,代码会将“ 0.00€”产品添加到购物车中,因此我以后可以将等值的钱退还给买方。 With jQuery when I click on the link nothing happens at all. 使用jQuery,当我单击链接时,什么也没有发生。 So it's not a jQuery related problem... I might be doing the .html() wrong... :( help! 因此,这不是与jQuery相关的问题...我可能做的.html()错误... :(帮助!

Thanks! 谢谢!

try to use the append() method : 尝试使用append()方法:

$('div.cupao_confirm').append("<div class="simpleCart_shelfItem"><h1 class="item_name">Discount of 30%!</h1><a href="javascript:;" class="item_add">Confirm Coupon!</a><span class="item_price" >0.00€</span></div>");

Give it a try and let me know if it worked, Thanks. 试试看,让我知道它是否有效,谢谢。

I think maybe you need to re-initialize simple cart ager changing the shell contents. 我认为也许您需要重新初始化更改外壳内容的简单购物车。 Try: 尝试:

            simpleCart.init();

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

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