简体   繁体   English

将事件监听器添加到HTML span标签

[英]Adding event listener to HTML span tag

I have a shopping cart in an ecommerce and when the value of the shopping cart alters a banner appears different for each value. 我在电子商务中有一个购物车,当购物车的价值发生变化时,横幅广告的每个值看起来都不同。

The js will just run and show the correct banner when the page reload one (this occurs when the customer adds another product in the cart). 当页面重新加载js时,js会运行并显示正确的横幅(当客户在购物车中添加另一种产品时发生)。

But when the client only changes the amount of products or remove a product from the cart page does not reload the one then thought of using the event listener. 但是,当客户仅更改产品数量或从购物车页面中删除产品时,不会重新加载该产品,于是想到使用事件侦听器。

Except my problem is that the value ($) is inside a tag <span> $ value </ span> and not within an imput, so I can not make the event listener function. 除了我的问题外,值($)在标记<span> $ value </ span>内,而不是在imput内,因此我无法使事件侦听器起作用。

How can I apply this? 我该如何应用? The code is below: 代码如下:

jQuery(document).ready(function(){
 setTimeout(function(){
    trocaBannerCarrinho();
 }, 5000);

//here is the event listener that does not work!! 
jQuery("#value_total").on("change", function(){
   trocaBannerCarrinho();
}); 
});


function trocaBannerCarrinho(){

// condições para exibir os banners
var valor_cond1 = {min: 1, max: 980};//banner 1
var valor_cond2 = {min: 981, max: 1280};//banner 2
var valor_cond3 = {min: 1281, max: 1580};//banner 3
var valor_cond4 = {min: 1581, max: 1980};//banner 4
var valor_cond5 = {min: 1981};//banner 5

// recuperando o valor do carrinho
var total_carrinho = jQuery("#value_total").html();

// removendo R$, o ponto e trocando a virgula por um ponto
// o resultado final fica assim: 35.00, 1900.99, 20000.58, etc...
total_carrinho = total_carrinho.replace(/\s/g,'').replace('R$','').replace('.','').replace(',','.');

if ((total_carrinho >= valor_cond1.min) && (total_carrinho <= valor_cond1.max)) {
    jQuery('iframe[id^="google_ads_iframe_"]').contents().find('#banner1').show();

} else if ((total_carrinho >= valor_cond2.min) && (total_carrinho <= valor_cond2.max)) {
    jQuery('iframe[id^="google_ads_iframe_"]').contents().find('#banner2').show();

} else if ((total_carrinho >= valor_cond3.min) && (total_carrinho <= valor_cond3.max)) {
    jQuery('iframe[id^="google_ads_iframe_"]').contents().find('#banner3').show(); 

} else if ((total_carrinho >= valor_cond4.min) && (total_carrinho <= valor_cond4.max)) {
    jQuery('iframe[id^="google_ads_iframe_"]').contents().find('#banner4').show();

} else if (total_carrinho >= valor_cond5.min) {
    jQuery('iframe[id^="google_ads_iframe_"]').contents().find('#banner5').show();
}
}

My Guess is, the listener is not getting updated. 我的猜测是,监听器没有更新。 Try doing 尝试做

jQuery("document").on("change", '#value_total' ,function(){
   trocaBannerCarrinho();
}); 

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

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