"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]
So it is basically separating behavior or javascript from presentation or 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');
};
That time we have separated javascript from html with a very basic example.
Note:
There is more to unobstrusive javascript as can be checked out on wikipedia article.
发布评论
评论(1)
查看维基百科文章:
所以它基本上将行为或 javascript 与表示或 html 分开。
示例:
这不是一个不引人注目的 JavaScript,因为行为和表示是混合的。
onclick
不应该出现在 html 中,而应该是 javascript 本身而不是 html 的一部分。通过上面的例子,你可以像这样不显眼:
JavaScript:
这次我们用一个非常基本的例子将 javascript 与 html 分开。
注意:
还有更多关于不显眼的 JavaScript 的内容,可以在维基百科文章中查看。
Checkout the wikipedia article:
So it is basically separating behavior or javascript from presentation or html.
Example:
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:
JavaScript:
That time we have separated javascript from html with a very basic example.
Note:
There is more to unobstrusive javascript as can be checked out on wikipedia article.