2010-11-30 43 views
2

所以我是一個完整的新手,並堅持條件語句。這裏是我的代碼:新手卡在一個jQuery的問題......條件語句

<script type="text/javascript"> 
$(document).ready(function(){ 
if($('span.fc_cart_item_price_total') == 0) { 
    $(span.fc_info).addClass('foo'); 
}; 
}); 
</script> 

所以我嘗試看看跨度一類的「fc_cart_item_price_total」具有「0」值,以再一類「富」的加入跨度帶班「.fc_info」。上面的代碼不起作用。這裏的HTML:

<span class="fc_info">Info 1</span><br /> 
<span class="fc_cart_item_price_total">$0.00</span><br /> 
<span class="fc_info">Info 2</span> 

這是我的另一個挑戰。我試圖在「fc_cart_item_price_total」類的跨度之前選擇值爲「fc_info」的跨度,但不知道如何選擇這個跨度。

+0

@user,你的文字有$ 0.00包裝權,你需要將其轉換成一個零對比 – kobe 2010-11-30 08:25:21

+0

之前,請記住,`$(選擇)`返回一個*新的jQuery對象*(如不爲0或「$ 0.00」或任何其他任意)。 – 2010-11-30 08:25:24

回答

1

對於價格爲0.00美元的每個購物車項目,這會將「foo」類添加到前面的(並且僅前面的)fc_info中。

$(function() { 
    $(".fc_cart_item_price_total:contains($0.00)").each(function(i, n) { 
     (n=$(n)).prevAll(".fc_info:first").addClass("foo"); 
    }); 
}); 
0

我認爲你需要從跨度中獲取HTML。例如:

<script type="text/javascript"> 
$(document).ready(function(){ 
if($('span.fc_cart_item_price_total').text() == 0) { 
    $(span.fc_info).addClass('foo'); 
}; 
}); 
</script> 

但是之後您需要將它與「$ 0.00」進行比較或在比較之前將$關閉。

+0

你在代碼中有兩個錯誤,.get('html')將得到只有文本,第二件事是fc_info需要.class名... – kobe 2010-11-30 08:22:30

+0

謝謝,政府。我發現了text()錯誤並修復了它。我在下面看到你的答案,所以我只是把我的問題留下來,這樣你就可以得到正確答案的功勞。 :-) – 2010-11-30 08:25:05

0

下面應該工作...請嘗試

<script type="text/javascript"> 
    $(document).ready(function(){ 
    if($('.fc_cart_item_price_total').text() == 0) { 
     $('.fc_info').addClass('foo'); 
    }; 
    }); 
    </script> 
0

爲了讓上面的代碼工作

<script type="text/javascript"> 
$(document).ready(function(){ 
if($('span.fc_cart_item_price_total').html() == 0) { 
    $(span.fc_info:first).addClass('foo'); 
}; 
}); 
</script> 

,並選擇第一類fc_info

使用$(".fc_info:first")爲您選擇

2

與0比較不是最好的臨時表你可以做。試試這個:

if (!parseFloat($('.fc_cart_item_price_total').text()) { 
.... 

根據您期望的數據使用ParseInt或ParseFloat。