开发者

jquery: If url is ____ change class

HELP! lol i cant figure it out!! aaarrrghhhh

heres what i got

HTML:

<html xmlns="http://www.w3.org/1999/xhtml" class="bg1">

Jquery:

$(document).ready(function(){
  var identifier = window.location.pathname;
  switch(identifier)
        {
            case: 'where-to-buy-our-goods.php';
            $('html').removeClass('bg1').addClass('bg2');
            break;
            case: 'about.php';
            $('html').removeClass('bg1').addClass('bg3');
            break;
            case: 'press.php';
            $('html').removeClass('bg1').addClass('bg4');
            break;
         开发者_开发技巧   case: 'contact.php';
            $('html').removeClass('bg1').addClass('bg5');
            break;
        }

});

also i think this might have something to do with it...

my site is in a folder on the root as a tester...

so the url is www.URL.com/Folder/about.php for exapmle... idk if this changes what the window.location.pathname should look like...

please help


case "index.php":

NOT

case: "index.php;


In order to decide off the path of the url bar:

$(document).ready(function(){
  var identifier = window.location.pathname;
  switch(identifier)
        {
            case: 'index.php';
            //edit proper tag's css
            $('body').css('background','img.jpg');
            break;
            case: 'about.php';
            //edit proper tag's css
            $('body').css('background','img2.jpg');
            break;
        }

});

Substitute body for the proper tag.

Perhaps though, there are better alternatives to this... couldn't you have different body classes to change the background image depending on what page you are on?


Get URL:

Get current URL in JavaScript?

You can then use the switch/case statement:

http://javascript.about.com/library/bltut05.htm


Lets suppose you wanted to add the background to the body. I will first suggest 2 CSS ways, then if none work for you, I'll suggest a JavaScript way.

<style type="text/css">
   body{ background:url(bg1.jpg) };
</style>

And you would change bg1.jpg in each HTML file.

<body style="background:url(bg1.jpg)">

Same for this one. And JavaScript (jQuery):

$(document).ready(function(){
   switch(window.location.pathname){
      case "index.php":
        $("body").css("background","url(bg1.jpg)");
      break;
   }
});

And you would add how many cases you need.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜