英文:
HTML/CSS/JS Multiple pages of HTML in one file
问题
我是你的中文翻译。以下是翻译好的部分:
我对HTML/CSS/JS相当新(大约3个月)。我目前正在尝试创建一个电子商务网站,但我在产品页面遇到了一个问题。每当您单击一个产品时,它应该带您到一个产品详细信息页面,显示诸如名称、价格、图片和添加到购物车按钮等内容。我有相当多的产品(约25个产品),因此为每个产品创建一个.html文件并不理想。所以我想知道是否可能将所有产品页面放在一个HTML文件中。
这是包含产品详细信息页面所有细节的部分,当您单击产品图片时加载。
这是我使图片可点击的方式(这在一个单独的HTML文件中):
<div class="row">
<div onclick="window.location.href='product-details.html'" class="col-4">
<img src="Images/asus3080.png" />
<h4>ASUS ROG STRIX GeForce RTX 3080 V2 10GB</h4>
<p>€800.00</p>
</div>
</div>
我尝试过查找如何做到这一点,但大多数方法都是使用插件(我不太确定如何使用)。
英文:
I'm fairly new to HTML/CSS/JS (about 3 months now). I'm currently trying to create an e-commerce website but i'm having an issue with my products page. Whenever you click a product, It's supposed to take you to a product details page showing things such as the name, price, a picture, and an add to cart button. I have a quite a few products (~25 products) so it wouldn't be Ideal to create a .html file for every single product. So i wanted to know if it was possible to have all the product pages in one HTML file.
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-html -->
<div class="small-container single-product">
<div class="row">
<div class="col-2 col-6" id="gpu1">
<img src="Images/strix3080.png" width="70%">
</div>
<div class="col-2 col-6" id="gpu1">
<p>Home / Products / ROG STRIX GeForce RTX 3080</p>
<h1>ASUS ROG STRIX GeForce RTX 3080 V2 10GB</h1>
<h4>&euro;800.00</h4>
<input type="number" value="1" max="5">
<a href="" class="btn add-cart">Add To Cart</a>
<h3>Specifications<i class="fa fa-indent" onclick="toggleSpecs()"></i></h3>
<br>
<div id="specs">
<p>
Bus Standard: PCI Express 4.0<br> Video Memory: 10GB GDDR6X<br> Engine Clock: 1440 MHz (Base Clock) 1935 MHz (Boost Clock)<br> CUDA Core: 8704<br> Memory Speed: 19 Gbps<br> Interface: Yes x2 (Native HDMI 2.1), Yes x3 (Native DisplayPort 1.4a),
HDCP Support Yes (2.3)<br> Resolution: Digital Max Res. 7680 x 4320<br> Dimensions: 12.53" x 5.51" x 2.27" Inch 31.85 x 14.01 x 5.78 Centimeter<br> Recommended PSU: 850W<br> Power Connectors: 3 x 8-Pin
</p>
</div>
</div>
</div>
</div>
<!-- end snippet -->
This is what contains all the details for the product details page and what is loaded when you click on a product's picture.
This is how I'm making the picture clickable (This is in a separate HTML file):
<div class="row">
<div onclick="window.location.href='product-details.html'" class="col-4">
<img src="Images/asus3080.png" />
<h4>ASUS ROG STRIX GeForce RTX 3080 V2 10GB</h4>
<p>&euro;800.00</p>
</div>
</div>
I've tried looking up how to do this but most of them are to use plugins (which im not rlly sure how to use).
答案1
得分: 1
如果你想在单个页面上显示所有产品,一种不需要后端代码的解决方案是使用JavaScript来迭代每个产品,并基于此生成HTML。
const productsArray = [
{
name: "华硕电脑",
image: "asus3080.png",
price: 50
},
{
name: "联想电脑",
image: "Lenovo.png",
price: 50
},
{
name: "Macbook",
image: "Mac.png",
price: 50
}
]
const root = document.getElementById("root")
for (let i = 0; i < productsArray.length; i++) {
root.innerHTML += `<div class="row">
<div onclick="window.location.href='product-details.html'" class="col-4">
<img src="Images/${productsArray[i].image}">
<h4>${productsArray[i].name}</h4>
<p>${productsArray[i].price}</p>
</div>`;
}
如果你想使用模板为每个产品生成一个页面,你需要编写后端代码,服务器将提供API或生成网页。
你可以使用诸如PHP、Java、C#以及后端JavaScript(NodeJS)等许多语言来实现这一点。
你可以通过在URL中使用GET
参数来模拟拥有每个产品的页面。
示例:https://localhost/?product=1
const productsArray = [
{
id: 1,
name: "华硕电脑",
image: "asus3080.png",
price: 50
},
{
id: 2,
name: "联想电脑",
image: "Lenovo.png",
price: 50
},
{
id: 3,
name: "Macbook",
image: "Mac.png",
price: 50
}
]
// 这是一个示例
const parameterList = new URLSearchParams("?product=1")
const productId = parameterList.get("product")
const root = document.getElementById("root")
const product = productsArray.find(p => p.id == productId);
console.log(productId)
root.innerHTML += `<div class="row">
<div onclick="window.location.href='product-details.html'" class="col-4">
<img src="Images/${product.image}">
<h4>${product.name}</h4>
<p>${product.price}</p>
</div>`;
英文:
If you want to show all products on a single page, one solution that doesn't require back-end code, as you haven't worked with server-side code yet, is to use JavaScript to iterate over every product and generate HTML based on that.
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
const productsArray = [
{
name: "ASUS computer",
image: "asus3080.png",
price: 50
},
{
name: "Lenovo computer",
image: "Lenovo.png",
price: 50
},
{
name: "Macbook",
image: "Mac.png",
price: 50
}
]
const root = document.getElementById("root")
for (let i = 0; i < productsArray.length; i++) {
root.innerHTML += `<div class="row">
<div onclick="window.location.href='product-details.html'" class="col-4">
<img src="Images/${productsArray[i].image}">
<h4>${productsArray[i].name}</h4>
<p>${productsArray[i].price}</p>
</div>`
}
<!-- language: lang-html -->
<div id="root"></div>
<!-- end snippet -->
If you mean to use a template to generate a page for every product, you are going to need to write back-end code where your server will either serve an API or generate the web pages.
There are many languages like PHP, Java, C# and also back-end JavaScript (NodeJS) you can use to achieve this.
You could fake having a page for every product, by using the GET
parameters in your URL.
Example: https://localhost/?product=1
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
const productsArray = [
{
id: 1,
name: "ASUS computer",
image: "asus3080.png",
price: 50
},
{
id: 2,
name: "Lenovo computer",
image: "Lenovo.png",
price: 50
},
{
id: 3,
name: "Macbook",
image: "Mac.png",
price: 50
}
]
// This si for the example
const parameterList = new URLSearchParams("?product=1")
const productId = parameterList.get("product")
const root = document.getElementById("root")
const product = productsArray.find(p => p.id == productId);
console.log(productId)
root.innerHTML += `<div class="row">
<div onclick="window.location.href='product-details.html'" class="col-4">
<img src="Images/${product.image}">
<h4>${product.name}</h4>
<p>${product.price}</p>
</div>`
<!-- language: lang-html -->
<div id="root"></div>
<!-- end snippet -->
答案2
得分: 0
"由于您没有使用后端,可以通过更改您的URL来将每个产品的数据传递到同一页面,如下所示:
window.location.href='product-details.html?product_name=Iphone&product_price=1000'
在product-details.html文件中,您可以使用JavaScript从URL获取数据,参考此链接 https://flaviocopes.com/urlsearchparams/。
然后使用JavaScript将这些数据放入HTML中。
希望这能帮助您解决问题。"
英文:
As you are not using backend, for passing data of each product to same page you can change your url like this
"window.location.href='product-details.html?product_name=Iphone&product_price=1000'"
and in product-details.html file you can fetch data from url using js,
refer this https://flaviocopes.com/urlsearchparams/
and place these data inside html using js.
Hope this will help you to solve your issue.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论