简体   繁体   English

在JavaScript中构建HTML的最佳实践

[英]Best practice for HTML building in JavaScript

Long story short, this right here seems incredibly wonky to me (not the actual code but the way it's written): 长话短说,这对我来说似乎令人难以置信(不是实际的代码,而是它的编写方式):

var B_ProgressBar = '<span style="border-style:solid;border-width:1px;background-color:#FFFFFF;width:100px;display:inline-block;" onmouseover="PBtext'
+ int + '.innerHTML=\'' + chaptersremaining + "/" + chapterstotal + '\';PBgreen' + int + '.style.width=\'' + chppercent + '%\'" onmouseout="PBtext'
+ int + '.innerHTML=\'' + Npercentage + '%\';PBgreen' + int + '.style.width=\'' + Npercentage + '%\'"><span id="PBgreen' + int
+ '" style="background-color:#01CA24;width:' + Npercentage + '%;display:inline-block;"><span id="PBtext' + int + '" style="display:inline-block;">'
+ Npercentage + '%</span></span></span>';

Lots of + and lots of escaped quotation marks. 大量的+和大量的转义引号。

I come from a Java world, where overuse of string concatenation is usually the last thing you want. 我来自Java世界,过度使用字符串连接通常是你想要的最后一件事。

Yet it's the only solution I can think of for JS. 然而,这是我能为JS思考的唯一解决方案。 I know frameworks like AngularJS and possibly jQuery take the nastiness out of such disgusting code using templates and inject the proper code without having to worry about missing a closing pointy bracket at some point. 我知道像AngularJS和可能的jQuery这样的框架会使用模板从这些令人作呕的代码中获取肮脏并注入正确的代码,而不必担心在某些时候错过了一个关闭尖括号。

Doesn't JS have some built-in functionality to build HTML blocks without having to resort to third party libraries? JS是否有一些内置功能来构建HTML块而不必诉诸第三方库? Something like an "Hypertext builder", which allows one to pass a couple parameters into a mask and the function spits out a nice, ready to use div block or something. 像“超文本构建器”之类的东西,它允许一个人将一对参数传递给一个掩码,并且该函数吐出一个漂亮的,准备使用的div块或其他东西。

Think of prepared statements for SQL or printf() in C. 在C中考虑SQL或printf()的预准备语句。

The answer is yes. 答案是肯定的。 The DOM (Document Object Model) is an API served for exactly this purpose. DOM(文档对象模型)是用于此目的的API。

As a first step, look into the following functions: 作为第一步,请查看以下功能:

These will allow you to create HTML elements, and append them to the DOM. 这些将允许您创建HTML元素,并将它们附加到DOM。

For more reading on the DOM, an essential subject to understand in Web development , read this: https://developer.mozilla.org/en-US/docs/Web/API/Document_Object_Model/Introduction 有关DOM的更多阅读,这是Web开发中必不可少的主题 ,请阅读: https//developer.mozilla.org/en-US/docs/Web/API/Document_Object_Model/Introduction

Last point I would recommend is to read MDN over W3 Schools, if you want an in depth analysis on how an API in JS works! 如果你想深入分析JS中API的工作方式,我建议的最后一点是阅读W3学校的MDN!

JavaScript supports Template Literals : JavaScript支持模板文字

 var a = 5; var b = 10; console.log(`Fifteen is ${a + b} and not ${2 * a + b}.`); 

I would lean towards using DOM Manipulation and using createElement and friends rather then generating raw HTML though. 我倾向于使用DOM操作并使用createElement和朋友,而不是生成原始HTML。

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

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