简体   繁体   English

组织 HTML 和 JavaScript 文件

[英]Organizing HTML and JavaScript files

I know that it is a good practice to organize files by separating them into files.我知道通过将文件分成文件来组织文件是一种很好的做法。 ie, files.html and files.js .即, files.html 和 files.js 。 However, I have lately been looking at some websites and the way their files are organized.但是,我最近一直在查看一些网站及其文件的组织方式。 They tend to contain tiny bits of JavaScript inserted here and there inside the html file.它们往往包含在 html 文件中随处插入的一小段 JavaScript。

Now, these files tend to be organized in a very particular way, one thing I have noticed is how I don't see much of document.getElementById(id) or ids.现在,这些文件往往以一种非常特殊的方式组织起来,我注意到的一件事是我看不到很多document.getElementById(id)或 ids。 So, I am curious to know what is the best way to grab the elements using JavaScript.所以,我很想知道使用 JavaScript 获取元素的最佳方法是什么。 Should I grab the elements in the html file or in the JavaScript file, and how?我应该获取 html 文件中的元素还是 JavaScript 文件中的元素,以及如何获取?

For instance take as an example the when a question is posted in Stack Overflow.以在 Stack Overflow 中发布问题的时间为例。 Similar results are shown.显示了类似的结果。 What would the JavaScript would look like if I have something like the following:如果我有以下内容,JavaScript 会是什么样子:

 /* SOMETHING LIKE THIS but this would mean that I would have to grab the elements everytime the event is fired (not sure this is a good idea) */ var question; var similar_question; var ask_your_question; var maybe_already_answered; Questions.checkSimilarQuestions = function(){ question = document.getElementById('question'); similar_questions = document.getElementById('similar'); ask_your_question = document.getElementById('aks-your-question'); maybe_already_answered = document.getElementById('maybe-already-answered'); } /* OR MAYBE SOMETHING LIKE THIS */ (function(){ var question = document.getElementById('question'); var similar_questions = document.getElementById('similar'); var ask_your_question = document.getElementById('aks-your-question'); var maybe_already_answered = document.getElementById('maybe-already-answered'); /* Grab the rest of the elements and do something with them */ }())
 <div id='ask-your-question'> <input type='text' id='question'/> <div id='maybe-already-answered'> </div> </div> <div id='similar-questions'></div>

I am going to give this a try, but feel free to correct me.我将尝试一下,但请随时纠正我。

The above example shows 2 different things Object Orientated JavaScript and Functional JavaScript.上面的例子展示了面向对象的 JavaScript 和函数式 JavaScript 两种不同的东西。

A lot of inline JavaScript code is actually not inline in the static version of a web page but is either added by the server and / or a main JavaScript file.许多内联 JavaScript 代码实际上不是内联在网页的静态版本中,而是由服务器和/或主 JavaScript 文件添加的。

Another reason you may not see document.getElementById() is because a lot, and I mean almost everybody, uses jQuery which uses a different Syntax.您可能看不到document.getElementById()另一个原因是很多,我的意思是几乎每个人,都使用使用不同语法的 jQuery。 As far as I know there is no other way to fetch elements than document.getElement (id, class, tagname) with vanilla JavaScript.据我所知,除了使用 vanilla JavaScript 的 document.getElement (id, class, tagname) 之外,没有其他方法可以获取元素。 A jQuery declaration would look like jQuery 声明看起来像

$('#wrapper');

Best Practice?最佳实践?

Well.好。 I tend to declare elements I am going to be using as global variables.我倾向于声明我将要用作全局变量的元素。 I can imagine that a lot of developers do this since it's easier to write我可以想象很多开发人员都这样做,因为它更容易编写

searchForm.appendChild(someElement);

than

document.getElementById('#search-form').appendChild(someElement);

Every single time.每一次。

I hope this gave you some insight as to best practice's :)我希望这能让您对最佳实践有所了解:)

JSON example: JSON 示例:

var elements = {
    body: document.getElementsByTagname('body')[0],
    wrapper: document.getElementById('wraper')};

etc...等等...

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

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