gpt4 book ai didi

javascript 简单函数删除布局

转载 作者:行者123 更新时间:2023-11-30 09:04:08 24 4
gpt4 key购买 nike

我有一个 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);

最佳答案

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');
}

关于javascript 简单函数删除布局,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6614741/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com