HTML/CSS/JS 单个文件中的多个页面

huangapple go评论64阅读模式
英文:

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>&euro;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 -->

&lt;div class=&quot;small-container single-product&quot;&gt;
  &lt;div class=&quot;row&quot;&gt;
    &lt;div class=&quot;col-2 col-6&quot; id=&quot;gpu1&quot;&gt;
      &lt;img src=&quot;Images/strix3080.png&quot; width=&quot;70%&quot;&gt;
    &lt;/div&gt;
    &lt;div class=&quot;col-2 col-6&quot; id=&quot;gpu1&quot;&gt;
      &lt;p&gt;Home / Products / ROG STRIX GeForce RTX 3080&lt;/p&gt;
      &lt;h1&gt;ASUS ROG STRIX GeForce RTX 3080 V2 10GB&lt;/h1&gt;
      &lt;h4&gt;&amp;euro;800.00&lt;/h4&gt;
      &lt;input type=&quot;number&quot; value=&quot;1&quot; max=&quot;5&quot;&gt;
      &lt;a href=&quot;&quot; class=&quot;btn add-cart&quot;&gt;Add To Cart&lt;/a&gt;
      &lt;h3&gt;Specifications&lt;i class=&quot;fa fa-indent&quot; onclick=&quot;toggleSpecs()&quot;&gt;&lt;/i&gt;&lt;/h3&gt;
      &lt;br&gt;

      &lt;div id=&quot;specs&quot;&gt;
        &lt;p&gt;
          Bus Standard: PCI Express 4.0&lt;br&gt; Video Memory: 10GB GDDR6X&lt;br&gt; Engine Clock: 1440 MHz (Base Clock) 1935 MHz (Boost Clock)&lt;br&gt; CUDA Core: 8704&lt;br&gt; Memory Speed: 19 Gbps&lt;br&gt; Interface: Yes x2 (Native HDMI 2.1), Yes x3 (Native DisplayPort 1.4a),
          HDCP Support Yes (2.3)&lt;br&gt; Resolution: Digital Max Res. 7680 x 4320&lt;br&gt; Dimensions: 12.53&quot; x 5.51&quot; x 2.27&quot; Inch 31.85 x 14.01 x 5.78 Centimeter&lt;br&gt; Recommended PSU: 850W&lt;br&gt; Power Connectors: 3 x 8-Pin
        &lt;/p&gt;
      &lt;/div&gt;
    &lt;/div&gt;
  &lt;/div&gt;
&lt;/div&gt;

<!-- 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):

&lt;div class=&quot;row&quot;&gt;
  &lt;div onclick=&quot;window.location.href=&#39;product-details.html&#39;&quot; class=&quot;col-4&quot;&gt;
    &lt;img src=&quot;Images/asus3080.png&quot; /&gt;
    &lt;h4&gt;ASUS ROG STRIX GeForce RTX 3080 V2 10GB&lt;/h4&gt;
    &lt;p&gt;&amp;euro;800.00&lt;/p&gt;
  &lt;/div&gt;
&lt;/div&gt;

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: &quot;ASUS computer&quot;,
    image: &quot;asus3080.png&quot;,
    price: 50
  },
  {
    name: &quot;Lenovo computer&quot;,
    image: &quot;Lenovo.png&quot;,
    price: 50
  },
  {
    name: &quot;Macbook&quot;,
    image: &quot;Mac.png&quot;,
    price: 50
  }
]

const root = document.getElementById(&quot;root&quot;)

for (let i = 0; i &lt; productsArray.length; i++) {
  root.innerHTML += `&lt;div class=&quot;row&quot;&gt;
                        &lt;div onclick=&quot;window.location.href=&#39;product-details.html&#39;&quot; class=&quot;col-4&quot;&gt;
                        &lt;img src=&quot;Images/${productsArray[i].image}&quot;&gt;
                        &lt;h4&gt;${productsArray[i].name}&lt;/h4&gt;
                        &lt;p&gt;${productsArray[i].price}&lt;/p&gt;
                    &lt;/div&gt;`
}

<!-- language: lang-html -->

&lt;div id=&quot;root&quot;&gt;&lt;/div&gt;

<!-- 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: &quot;ASUS computer&quot;,
    image: &quot;asus3080.png&quot;,
    price: 50
  },
  {
    id: 2,
    name: &quot;Lenovo computer&quot;,
    image: &quot;Lenovo.png&quot;,
    price: 50
  },
  {
    id: 3,
    name: &quot;Macbook&quot;,
    image: &quot;Mac.png&quot;,
    price: 50
  }
]

// This si for the example
const parameterList = new URLSearchParams(&quot;?product=1&quot;)
const productId = parameterList.get(&quot;product&quot;)

const root = document.getElementById(&quot;root&quot;)

const product = productsArray.find(p =&gt; p.id == productId);

console.log(productId)

root.innerHTML += `&lt;div class=&quot;row&quot;&gt;
                      &lt;div onclick=&quot;window.location.href=&#39;product-details.html&#39;&quot; class=&quot;col-4&quot;&gt;
                      &lt;img src=&quot;Images/${product.image}&quot;&gt;
                      &lt;h4&gt;${product.name}&lt;/h4&gt;
                      &lt;p&gt;${product.price}&lt;/p&gt;
                  &lt;/div&gt;`

<!-- language: lang-html -->

&lt;div id=&quot;root&quot;&gt;&lt;/div&gt;

<!-- 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

&quot;window.location.href=&#39;product-details.html?product_name=Iphone&amp;product_price=1000&#39;&quot;

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.

huangapple
  • 本文由 发表于 2023年4月17日 18:56:18
  • 转载请务必保留本文链接:https://go.coder-hub.com/76034396.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定