简体   繁体   English

如何避免 HTML 中的重复代码?

[英]How to avoid repetitive code in HTML?

Suppose i have a class as follows which shows the available items i want to sell on my website.假设我有一个如下类,它显示了我想在我的网站上出售的可用物品。 How should i avoid writing the same code again and again when i add new products to my site.当我向我的网站添加新产品时,我应该如何避免一次又一次地编写相同的代码。 Everytime i add a new product, only its image and the page where it points to will change.每次我添加一个新产品时,只有它的图像和它指向的页面会改变。 Is there any way to manage this efficiently ?有没有办法有效地管理这个? Thanks in advance提前致谢

<div class="sale">
        <div class="img-container"><a href="#"><img src="1.png">Product1</a></div>
        <div class="img-container"><a href="#"><img src="2.png">Product2</a></div>
        <div class="img-container"><a href="#"><img src="3.png">Product3</a></div>
        <div class="img-container"><a href="#"><img src="4.png">Product4</a></div>
</div>

basically first you have to learn any server side technologies (eg, Asp, NodeJS. PHP etc.) and database (eg, mysql, MS Sql server) then, I am sure you will get your answer. 基本上,首先您必须学习任何服务器端技术(例如Asp,NodeJS,PHP等)和数据库(例如mysql,MS Sql服务器),然后,我相信您会得到答案。 More importantly also learn Javascript or its one of library Jquery or preferably AngularJS . 更重要的是,还要学习Javascript或其库Jquery或最好是AngularJS之一 For creating dynamic html you have to command on Javascript at least 要创建动态html,您至少必须使用Javascript命令

There are lot many options to achieve DHTML. 有很多实现DHTML的选项。 A basic example using JavaScript: 使用JavaScript的基本示例:

 var details = [{ 'img': "http://simpleicon.com/wp-content/uploads/light-bulb-6.svg", 'lbl': "Product 1" }, { 'img': "http://images.clipartpanda.com/light-bulb-vector-png-9czEXKbbi.svg", 'lbl': "Product 2" }, { 'img': "http://images.clipartpanda.com/light-bulb-vector-png-light-bulb-7.svg", 'lbl': "Product 3" }]; var html = ""; for (var i = 0, l = details.length; l > i; i++) { html += "<div class=\\"img-container\\">"; html += "<a href=\\"#\\">"; html += "<img src=\\"" + details[i].img + "\\">" + details[i].lbl + "</a>"; html += "</div>"; } document.getElementsByClassName('sale')[0].innerHTML = html; 
 .sale { border: 1px ridge grey; } .img-container { border: 1px dashed grey; margin: 3px; display: inline-block; padding: 3px; } .img-container img { width: 100px; } 
 <div class="sale"> <div class="img-container"> <a href="#"> <img src="1.png">Product1</a> </div> <div class="img-container"> <a href="#"> <img src="2.png">Product2</a> </div> <div class="img-container"> <a href="#"> <img src="3.png">Product3</a> </div> <div class="img-container"> <a href="#"> <img src="4.png">Product4</a> </div> </div> 

Almost in Every Back-end Technologies like as Node.js , Django there is a concept called template .几乎在像 Node.js 和 Django 这样的每个后端技术中,都有一个叫做模板的概念。 If you know or learn template then you can avoid repetition of your html code.And you can re-use your html code in another html files.如果您知道或学习模板,那么您可以避免重复您的 html 代码。您可以在另一个 html 文件中重新使用您的 html 代码。

for Node.js template用于 Node.js模板

for Django template对于 Django模板

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM