什么是外行术语中的Unobtrusive Javascript? [英] What is Unobtrusive Javascript in layman terms?
问题描述
外行术语中什么是不显眼的Javascript?一个例子可以帮助我理解。
What is Unobtrusive Javascript in layman terms? An example would be nice to aid my understanding.
推荐答案
查看维基百科文章:
- Unobtrusive JavaScript
Unobtrusive JavaScript是在
网页中使用JavaScript的一般
方法。虽然该术语不是正式定义的
,但其基本原则
通常被理解为包括:
"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:
- 功能分离(行为层)来自网页的
结构/内容和表示 - 避免传统JavaScript
编程问题的最佳实践(如浏览器$) b $ b不一致且缺乏
可扩展性) - 支持用户代理的渐进增强功能可能不支持
支持高级JavaScript
功能[2]
所以它基本上将行为或javascript与演示文稿或html分开。
So it is basically separating behavior or javascript from presentation or html.
示例:
<input type="button" id="btn" onclick="alert('Test')" />
这不是一种不引人注目的javascript,因为行为和呈现是混合的。 onclick
不应该在html中,并且应该是javascript本身而不是html的一部分。
That is not unobstrusive javascript because behaviour and presentation are mixed. The onclick
shouldn't be there in html and should be part of javascript itself not html.
以上示例,你可以像这样不引人注目:
With above example, you can go unobstrusive like this:
<input type="button" id="btn" />
JavaScript:
var el = document.getElementById('btn');
el.onclick = function(){
alert('Test');
};
那时我们用一个非常基本的例子将javascript与html分开。
That time we have separated javascript from html with a very basic example.
注意:
还有更多不引人注意的javascript,可以在维基百科文章中查看。
There is more to unobstrusive javascript as can be checked out on wikipedia article.
这篇关于什么是外行术语中的Unobtrusive Javascript?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!