2011-02-15 44 views
0

幫助!大聲笑我不能弄明白! aaarrrghhhhjquery:如果url是____更改類

繼承人我得到了什麼

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; 
     } 

}); 

也是我認爲這可能有什麼關係呢?

我的網站位於根目錄下的文件夾中作爲測試人員...

這樣的URL是www.URL.com/Folder/about.php爲exapmle ... IDK的,如果這改變了window.location.pathname應該是什麼樣子......

請幫助

+4

有沒有你不直接將這些類到你感興趣的頁面中的原因是什麼?例如,在index.php上,添加一個將您的類設置爲bg1的腳本。 – 2011-02-15 21:22:36

+0

帶有html和body標籤的頭文件是php包含的,所以它需要更具動態性,我認爲 – Alex 2011-02-15 21:24:16

回答

0

爲了決定關閉地址欄的路徑:在適當的標籤

$(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; 
     } 

}); 

替代體。

也許雖然有更好的替代方案...你不能有不同的身體類來改變背景圖像取決於你在哪個網頁?

0

讓我們假設你想添加背景身體。我會第一個建議2種CSS方式,那麼如果沒有爲你工作,我會建議一種JavaScript方式。

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

而且您會在每個HTML文件中更改bg1.jpg

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

這一個相同。和JavaScript(jQuery):

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

然後你會添加多少cases你需要。

1

case "index.php":

case: "index.php;