2011-07-07 95 views
1

我有一個JQuery,Javascript,HTML網站。我做了一個簡單的函數來打擊文本,如果它不等於0.javascript簡單功能刪除佈局

該函數似乎工作,但頁面中的其他內容都消失了。我只看到了我的文字。

productPrice應striked和salePrice不罷工...該功能的作品,但佈局變得混亂。幫幫我 ?

的文本顯示像這樣我的網頁上:

<label id="productPrice">Price</label> 
     <label id="salePrice">Sale Price</label> 

繼承人的代碼,我可以根據需要提供更多。

功能

if (product.price != 0); 
{ 
    var salePrice = product.price; 
    document.write(salePrice.strike()); 
} 

陣列

var catalog = {"products": [ 
{"thumbnail": '/pub/3/resources/ti/store/mobile/chrome.png', 
    "brand": "Google", 
    "name": "Chrome", 
    "description": "Lorem ipsum dolor sit amet, consectetur adipiscing elit.", 
    "rating": '5', 
    "sale_price": "0.00$", 
    "sale_desc": "", 
    "price": "0.00$"}, 

參數

$('#productPrice').html(product.price); 

回答

5

document.write()是消除一切從網頁,但你寫的東西。這就是它的工作原理。您現在可以試用它,將javascript: document.write("Gone");放入您的地址欄中,頁面將以同樣的方式消失。

另外,你的if語句後面還有一個分號,所以你的代碼將始終運行。

if (product.price !== 0) { 
    document.getElementById('productPrice').style.textDecoration = 'line-through'; 
} 

if (product.price !== 0) { 
    $('#productPrice').css('text-decoration', 'line-through'); 
} 
+2

哦。這解釋了很多。切勿再次使用document.write()...謝謝。感謝幫助。 – JFFF