[英]How to keep CSS changes made with Jquery after ajax call or form call
Im trying to hide a couple of payment options on our lightspeed Ecommerce site using Jquery during a set timeframe (Between 18.00 and 24.00) and only for people living in the Netherlands.我试图在设定的时间范围内(18.00 和 24.00 之间)使用 Jquery 在我们的 lightspeed 电子商务网站上隐藏几个付款选项,并且仅适用于居住在荷兰的人。
Since i cannot alter the file of the checkout page i use document.querySelector("#gui-checkout-shipment-methods > div:nth-child(1) > div") to then alter the css of mentioned div's "display:block" to be set as "display:none".由于我无法更改结帐页面的文件,因此我使用 document.querySelector("#gui-checkout-shipment-methods > div:nth-child(1) > div") 来更改提到的 div 的“display:block”的 css " 设置为 "display:none"。
I tried wrapping the code in ajaxSuccess and ajaxComplete funtion, but it did not change anything.我尝试将代码包装在 ajaxSuccess 和 ajaxComplete 函数中,但它没有改变任何东西。
$( document ).ajaxSuccess(function () {
$.get("https://freegeoip.app/json/", function (response) {
$("#ip").html("IP: " + response.ip);
$("#country_code").html(response.country_code);
var currentTime = new Date().getHours();
if(response.country_code=='NL' && 17 <= currentTime&¤tTime < 24){
document.querySelector("#gui-checkout-shipment-methods > div:nth-child(1) > div").style.display = 'none';
document.querySelector("#gui-checkout-shipment-methods > div:nth-child(2) > div").style.display = 'none';
document.querySelector("#gui-checkout-shipment-methods > div:nth-child(3) > div").style.display = 'none';
document.querySelector("#gui-checkout-shipment-methods > div:nth-child(4) > div").style.display = 'none';
document.querySelector("#gui-checkout-shipment-methods > div:nth-child(5) > div").style.display = 'none';
}
if(response.country_code=='NL' && 24 <= currentTime&¤tTime < 17){
document.querySelector("#gui-checkout-shipment-methods > div:nth-child(1) > div").style.display = 'block';
document.querySelector("#gui-checkout-shipment-methods > div:nth-child(2) > div").style.display = 'block';
document.querySelector("#gui-checkout-shipment-methods > div:nth-child(3) > div").style.display = 'block';
document.querySelector("#gui-checkout-shipment-methods > div:nth-child(4) > div").style.display = 'block';
document.querySelector("#gui-checkout-shipment-methods > div:nth-child(5) > div").style.display = 'block';
}
}, "jsonp");});
It works fine, but as soon as a Ajax form radio-button is clicked, my code stops working.它工作正常,但只要单击 Ajax 表单单选按钮,我的代码就会停止工作。 Inspector says: css-change-by-time.js?20190925175433:8 Uncaught TypeError: Cannot read property 'style' of null
检查员说:css-change-by-time.js?20190925175433:8 Uncaught TypeError: Cannot read property 'style' of null
This makes sense, since i guess after the ajax call is made, the div that used to be display:block is no longer there, and so it conflicts and throws the error.这是有道理的,因为我猜在 ajax 调用之后,曾经是 display:block 的 div 不再存在,因此它发生冲突并引发错误。
I searched the entire day on the internet, but could not find a working solution.我在互联网上搜索了一整天,但找不到有效的解决方案。
So my actual question is:所以我的实际问题是:
How do i apply these changes and how do i make them stick, no matter what happens.无论发生什么,我如何应用这些更改以及如何使它们坚持下去。
You first see the.html whether it is pointing right div or not.您首先看到.html 是否指向正确的div。 I am using jquery for this.
我为此使用 jquery。
$("#gui-checkout-shipment-methods > div:nth-child(1) > div").html()
Apply style display block if it is pointing right如果指向右侧,则应用样式显示块
$("#gui-checkout-shipment-methods > div:nth-child(1) > div").css('display','block');
apply display none as well也应用显示无
$("#gui-checkout-shipment-methods > div:nth-child(1) > div").css('display','none');
use this for all lines.将此用于所有行。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.