用外行人的话说,什么是 Unobtrusive Javascript?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 
原文地址: http://stackoverflow.com/questions/4478795/
Warning: these are provided under cc-by-sa 4.0 license.  You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What is Unobtrusive Javascript in layman terms?
提问by Imran
What is Unobtrusive Javascript in layman terms? An example would be nice to aid my understanding.
用外行人的话说,什么是 Unobtrusive Javascript?一个例子会很好地帮助我理解。
回答by Sarfraz
Checkout the wikipedia article:
查看维基百科文章:
"Unobtrusive JavaScript" is a general approach to the use of JavaScript in web pages. Though the term is not formally defined, its basic principles are generally understood to include:
- Separation of functionality (the "behavior layer") from a Web page's structure/content and presentation
 - Best practices to avoid the problems of traditional JavaScript programming (such as browser inconsistencies and lack of scalability)
 - Progressive enhancement to support user agents that may not support advanced JavaScript functionality[2]
 
“Unobtrusive JavaScript”是在网页中使用 JavaScript 的通用方法。虽然该术语没有正式定义,但其基本原则通常被理解为包括:
- 功能(“行为层”)与网页结构/内容和表现的分离
 - 避免传统 JavaScript 编程问题(例如浏览器不一致和缺乏可扩展性)的最佳实践
 - 逐步增强以支持可能不支持高级 JavaScript 功能的用户代理[2]
 
So it is basically separating behavior or javascript from presentation or html.
所以它基本上是将行为或 javascript 与演示或 html 分开。
Example:
例子:
<input type="button" id="btn" onclick="alert('Test')" />
That is not unobstrusive javascript because behaviour and presentation are mixed. The onclickshouldn't be there in html and should be part of javascript itself not html.
这不是不引人注目的 javascript,因为行为和演示是混合的。本onclick不应该出现在HTML,应该是JavaScript的本身不是HTML的一部分。
With above example, you can go unobstrusive like this:
通过上面的例子,你可以像这样不引人注目:
<input type="button" id="btn" />
JavaScript:
JavaScript:
var el = document.getElementById('btn');
el.onclick = function(){
  alert('Test');
};
That time we have separated javascript from html with a very basic example.
那个时候我们用一个非常基本的例子将 javascript 与 html 分开。
Note:
笔记:
There is more to unobstrusive javascript as can be checked out on wikipedia article.
可以在维基百科文章中查看更多不引人注目的 javascript。

