简体   繁体   English

更具模块化; 在.js文件中包含html元素?

[英]More modular; include html elements in .js file?

I have a page which has many buttons that perform an action with javascript. 我有一个页面,其中包含许多按钮,这些按钮可以使用javascript执行操作。 No problem here, just a html page and multiple .js file, one .js file for every action. 没问题,只有一个html页面和多个.js文件,每个动作一个.js文件。 Just to separate stuff a bit and make everything a bit more manageable. 只是将内容分开一点,使所有内容都更易于管理。 I will write a script to combine them all at deployment. 我将编写一个脚本以在部署时将它们全部合并。

A lot of my javascript actions need html elements to function. 我的许多JavaScript操作都需要html元素才能起作用。 For example to change a color the color.js needs a div with form elements so that user can change the color and press the button to save changes. 例如,要更改颜色,color.js需要一个带有表单元素的div,以便用户可以更改颜色并按下按钮以保存更改。 The color.js binds functions to the onClick of the buttons and such. color.js将函数绑定到按钮的onClick等。 No problem here. 没问题

A problem comes up when there are many javascript actions using many html elements. 当使用许多html元素的许多javascript操作时出现问题。 All these html elements need to put into one html page. 所有这些html元素都需要放在一个html页面中。 This makes managing things complicated. 这使得管理事情变得复杂。 For example when i change the color field id i need to go to color.js and do the change. 例如,当我更改颜色字段ID时,我需要转到color.js并进行更改。 In addition i have to find the html element in my huge html page and also do the change. 另外,我必须在巨大的html页面中找到html元素,并进行更改。

Is it possible to put the html elements needed by my javascript actions into the .js file and somehow include it in my html page? 是否可以将我的javascript操作所需的html元素放入.js文件,并以某种方式将其包含在我的html页面中?

Of course i can escape all html elements and put it in a javascript variable and document.write it. 当然,我可以转义所有html元素并将其放在javascript变量中并进行document.write。 But escaping is not a nice thing to do and changing html afterwards is quite painful. 但是转义并不是一件好事,事后更改html会很痛苦。

I'm also not looking for a ajax function, cause with many different actions this will cause network problems. 我也不是在寻找Ajax函数,因为执行许多不同的操作会导致网络问题。

Cheers for any thoughts on the subject! 对此主题的想法加油!

You can use templates to generate dynamic HTML via JavaScript. 您可以使用模板通过JavaScript生成动态HTML。 Most of Templates Engines have the same implementation: 大多数模板引擎具有相同的实现:

  • A template structure (DOM) or HTML string 模板结构(DOM)或HTML字符串
  • A method to render the template 呈现模板的方法
  • The data that fills the template (Array, Object) 填充模板的数据(数组,对象)
  • Settings to configure the template engine 设置以配置模板引擎

I have used some template engines, one of them is Hogan.js which has a friendly sugared-syntax, very easy to use, but lower performance. 我使用了一些模板引擎,其中之一是Hogan.js ,它具有友好的加糖语法,非常易于使用,但性能较低。 It implements the Mustache syntax. 它实现了Mustache语法。

Other template engine is in Underscore.js library, which provides no sugared-syntax but a native javascript syntax, giving more power, and best performance. 其他模板引擎位于Underscore.js库中,该库不提供含糖语法,而是提供本机JavaScript语法,从而提供了更多功能和最佳性能。

The last one that I have used is kendo-ui templates . 我使用的最后一个是kendo-ui模板
Kendo UI templates have a heavy emphasis on performance over feature glut. Kendo UI模板非常注重性能而不是功能过剩。 It will trade convenient "syntax sugar" for improved performance. 它将使用方便的“语法糖”进行交易以提高性能。

If you want to try more engines, here you can get a lot of them: 如果您想尝试更多的引擎,可以在这里获得很多:


By other hand, if you want just to load static HTML (eg an static view), you can use jQuery.load(url) to load data from the server and place the returned HTML into the matched element. 另一方面,如果只想加载静态HTML(例如,静态视图),则可以使用jQuery.load(url)从服务器加载数据并将返回的HTML放入匹配的元素中。 .load() is a shorthand method of jQuery.ajax() .load()jQuery.ajax()的简写方法


Recomendation: whichever option you choose, I recommend using cache, either to save the view or to save the compiled template, thus the performance is improved by avoiding request of the same resource. 推荐:无论您选择哪个选项,我都建议使用缓存来保存视图或保存已编译的模板,因此可以通过避免请求相同的资源来提高性能。

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

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