2012-12-12 22 views
0

我正在使用以下代碼。第一個if條件正在工作,但第二個if不是。我不知道爲什麼?同樣的CSS也應該應用於第二個if。誰能告訴我爲什麼這是?在jquery中使用css

<script type="text/javascript"> 
    if (location.pathname.indexOf("girl-clothing") != -1) { 
     $(".RetailPriceValue").css("cssText", "margin: -203px 0 0 0 !important;"); 
    } 

    if (location.pathname.indexOf("http://www.xyz.com/girl-clothing/?sort=featured&page=2")!= -1) { 
     $(".RetailPriceValue").css("cssText", "margin:-220px 0 0 0 !important;"); 
    } 
</script> 
+0

那是什麼 'cssText' 屬性的名字嗎? –

+0

indexOf的轉義序列? –

回答

1

我會想象的第一個樣式變化是由巧合工作。你實際添加的是:

<div style="cssText: margin: -203px 0 0 0 !important;"></div> 

這裏的問題很明顯。試試這個:

if (location.pathname.indexOf("girl-clothing") != -1) { 
    $(".RetailPriceValue").css("margin", "-203px 0 0 0 !important;"); 
} 

if (location.pathname.indexOf("http://www.xyz.com/girl-clothing/?sort=featured&page=2")!= -1) { 
    $(".RetailPriceValue").css("margin", "-220px 0 0 0 !important;"); 
} 
+0

沒有它的不工作的第二個CSS不在那裏應用,它只是第一個CSS。爲什麼? – Rash

+0

嘗試刪除'!important'標誌。這種情況是否確實受到影響如果你在第二個if語句中加入alert(),它會出現嗎? –

+0

沒有警報沒有出現在那裏.. – Rash

1

使用類似

$(".RetailPriceValue").css("margin","-220px 0 0 0 !important"); 

所以會轉而

if (location.pathname.indexOf("girl-clothing") != -1) { 
    $(".RetailPriceValue").css("margin","-203px 0 0 0 !important"); 
} 

if (location.pathname.indexOf("http://www.xyz.com/girl-clothing/?sort=featured&page=2")!= -1) { 
    $(".RetailPriceValue").css("margin","-220px 0 0 0 !important"); 
} 
+0

我已經刪除它,但仍然第二個CSS不applied.atleast你能告訴我如何刪除「margin:-203px 0 0 0!important」在這個「http://www.xyz.com/girl-clothing/ ?sort = featured&page = 2「url ??? – Rash

+0

是不是看起來像流浪的'/'後的女孩服裝/?在網址? –

+0

你是什麼意思流浪?我認爲becoz女孩服裝它採取以前css.isnt它? – Rash