Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/72.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何在ajax调用或表单调用之后保留使用Jquery所做的CSS更改_Javascript_Jquery_Ajax - Fatal编程技术网

Javascript 如何在ajax调用或表单调用之后保留使用Jquery所做的CSS更改

Javascript 如何在ajax调用或表单调用之后保留使用Jquery所做的CSS更改,javascript,jquery,ajax,Javascript,Jquery,Ajax,我试图在一个设定的时间范围内(18.00和24.00之间)使用Jquery在我们的lightspeed电子商务网站上隐藏几个支付选项,并且只针对居住在荷兰的人 因为我无法更改签出页面的文件,所以我使用document.querySelector(#gui checkout-shipping-methods>div:nth-child(1)>div),然后将上述div的“display:block”的css更改为“display:none” 我尝试用ajaxSuccess和ajaxComplete

我试图在一个设定的时间范围内(18.00和24.00之间)使用Jquery在我们的lightspeed电子商务网站上隐藏几个支付选项,并且只针对居住在荷兰的人

因为我无法更改签出页面的文件,所以我使用document.querySelector(#gui checkout-shipping-methods>div:nth-child(1)>div),然后将上述div的“display:block”的css更改为“display:none”

我尝试用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&&currentTime < 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&&currentTime < 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");});
$(文档).ajaxSuccess(函数(){
$.get(”https://freegeoip.app/json/“,功能(响应){
$(“#ip”).html(“ip:+response.ip”);
$(“#国家代码”).html(response.country_代码);
var currentTime=new Date().getHours();

如果(response.country_code=='NL'&&17您首先会看到.html是否指向右div

 $("#gui-checkout-shipment-methods > div:nth-child(1) > div").html()
如果样式显示块指向右侧,则应用样式显示块

  $("#gui-checkout-shipment-methods > div:nth-child(1) > div").css('display','block');
也应用“无显示”

   $("#gui-checkout-shipment-methods > div:nth-child(1) > div").css('display','none');

对所有行都使用此选项。

最好的方法是在应用更改之前检查元素是否存在。这里有一个指向类似问题的链接,该问题有一个您可以应用的解决方案。此解决方案对dom选择器使用jQuery,您应该使用它,因为您已经在加载它。如果($(“#gui签出装运方法>div:nth child(1)>div”).length)$(“#gui签出装运方法>div:nth child(1)>div”).css('display','none');如果元素不存在怎么办?如果元素不存在,我给你的代码行将处理。因为如果元素不存在,if语句的计算结果将为false,并且它不会尝试将样式应用于不存在的元素。此外,我认为你的第二个if语句的计算结果不会为true。currentTime不能为gr饮食量低于24和17。这可能会给你带来问题。