Jquery for loop not working

时间:2015-09-01 22:34:56

标签: javascript jquery html

I am currently working on a site that does not allow me to edit some of the html content on each of the pages because it is part of a template. I am using Jquery to insert the divs on each page so that i can style them for later. I am currently stuck on this piece of code and cannot get it to work for me. Any suggestions would be greatly appreciated.

jQuery(document).ready(function() {

    var URLarray = {
        "/100.htm", "<div class='Plugins-div'>Volusion Plugins</div>";
        "/101.htm", "<div class='Pricing-div'>Pricing</div>";
        "/102.htm", "<div class='Services-div'>Services</div>";
        "/103.htm", "<div class='Contact-div'>Contact Us</div>";
    }

    jQuery.each(URLarray, function(key, value) {
            if (location.pathname.indexOf(key) > -1) {
                pageHTML = URLarray
                jQuery('div#navheader').after('<div id="top-banner" class="col-xs-12"><div class="text-center container">' + pageHTML + '</div></div>');
            }
        }
    });
});

1 个答案:

答案 0 :(得分:5)

Your code doesn't work (and causes an exception, check your console), because it is full of syntax errors. Object literal that looks like and array (and is named as an array), but has semicolons inside, misplaced closing curly braces, assignment of object where a string was probably meant to be used...

It's not entirely clear what you want to do, but this will probably work (and is at least valid code):

var URLarray = {
  "/100.htm": "<div class='Plugins-div'>Volusion Plugins</div>",
  "/101.htm": "<div class='Pricing-div'>Pricing</div>",
  "/102.htm": "<div class='Services-div'>Services</div>",
  "/103.htm": "<div class='Contact-div'>Contact Us</div>"
}

jQuery.each(URLarray, function(key, value) {
  if (location.pathname.indexOf(key) > -1) {
    jQuery('div#navheader').after('<div id="top-banner" class="col-xs-12"><div class="text-center container">' + value + '</div></div>');
  }
});