我有一个JQuery,Javascript,HTML网站。如果它不等于0,我做了一个简单的函数来打击文本。
该功能似乎有效,但页面中的其他所有内容都消失了。我只看到我的文字。
productPrice应该被打击并且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);
答案 0 :(得分: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');
}