简体   繁体   中英

Jquery calling function after ajax load fails

We have an external javascript file that gets called as part of a catalog results page, AJAX is involved on this document - https://www.columnradiators4u.co.uk/uk/classic-white-column-radiators.html

I have a jquery function for altering colour of a span when it detects certain text - please see my code below.

function ReInitStockColor(){
 $(function() {
  $("span:contains(In Stock)").css("font-weight", "bold").css("color", "#00BF00");
  $("span:contains(Due)").css("color", "#FF0000");
  $("span:contains(Made)").css("font-weight", "bold").css("color", "#00BF00");
 });
}

This code is being called earlier on in our file, however it is not executing. If I take my code in to chrome/Firefox console and run it, it executes successfully.

Is anybody able to assist in this matter?

EDIT: Full JS file here: https://jsfiddle.net/8z8oq2sm/ EDIT 08/01: Anybody able to offer any further help? I've tried the suggestions below to no avail.

$(function() { ... });

is just jQuery short-hand for

$(document).ready(function() { ... });

$(function() { ... }); suggests that code will be executed only on document ready. And you have it inside function ReInitStockColor. Can you remove that $(function() { ... }) thing and see if this ReInitStockColor function is being called?

您应该在AJAX 成功完成函数中调用此ReInitStockColor()函数

you need this?

   $.ajaxSetup({
        error: function(jqXHR, exception) {
            if (jqXHR.status === 0) {
                alert('Not connect.\n Verify Network.');
            } else if (jqXHR.status == 404) {
                alert('Requested page not found. [404]');
            } else if (jqXHR.status == 500) {
                alert('Internal Server Error [500].');
            } else if (exception === 'parsererror') {
                alert('Requested JSON parse failed.');
            } else if (exception === 'timeout') {
                alert('Time out error.');
            } else if (exception === 'abort') {
                alert('Ajax request aborted.');
            } else {
                alert('Uncaught Error.\n' + jqXHR.responseText);
            }
        }
    });

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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