0

我正在使用以下代码。第一个if条件有效,但第二个if条件无效。我不知道为什么?第二个也应该应用相同的 CSS if。谁能告诉我这是为什么?

<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>
4

3 回答 3

1

如果你的 location.href 是http://www.xyz.com/girl-clothing/?sort=featured&page=2

location.pathname 将是“/girl-clothing/”,因此 indexOf 函数永远不会找到字符串“http://”

https://developer.mozilla.org/en-US/docs/DOM/window.location

于 2012-12-12T11:24:12.650 回答
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");
}
于 2012-12-12T09:38:53.220 回答
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;");
}
于 2012-12-12T09:40:58.690 回答