帮助我理解 Unobtrusive JavaScript 的基本原理

发布于 2024-11-29 18:14:18 字数 1225 浏览 1 评论 0原文

我从 Wikipeia/Unobtrusive_JavaScript 中发现了 Unobtrusive JavaScript 的基本原则:

  • 功能分离(“行为层”) ") 来自网页的 结构/内容和演示
  • 避免的最佳实践 传统 JavaScript 编程的问题(例如浏览器 不一致和缺乏可扩展性)
  • 逐步增强 支持可能不支持高级 JavaScript 的用户代理 我可以轻松理解第一个功能

。我也可以理解第二个的避免缺乏可扩展性的问题。我想知道 Unobtrusive JavaScript 如何帮助避免浏览器不一致。相反,有时Obtrusive方式可以帮助避免浏览器不一致。例如,要向某个元素添加事件,我应该以不显眼的方式执行此操作:

<div id="button">Button</div>
<script>
var button = document.getElementById('button');

function buttonClick() {
  // do something
}

if (button.addEventListener) 
  button.addEventListener('click', buttonClick, false);
else
  button.attachEvent('onclick', buttonClick);
</script>

如果我这样做 显眼的方式:

<div id="button" onclick="buttonClick()">Button</div>
<script>
function buttonClick() {
  // do something
}
</script>

正如您所看到的显眼的方式更简单,不需要关心浏览器的不一致。那么您能否解释或向我展示任何示例,Unobtrusive 方式如何有助于避免浏览器不一致。

还有第三个。我可以以强制的方式进行渐进增强。例如,我仍然可以以强迫的方式使用 Modernizr

Unobtrusive JavaScript 如何帮助实现这些目标?

I found these basic principles of Unobtrusive JavaScript from Wikipeia/Unobtrusive_JavaScript:

  • 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

I can understand first one easily. I also can understand second one's avoid the problems of lack of scalability. I wonder how Unobtrusive JavaScript can help to avoid browser inconsistencies. On the contrary, sometimes Obtrusive way can help to avoid browser inconsistencies. For example, to add event to some element I should do this in Unobtrusive way:

<div id="button">Button</div>
<script>
var button = document.getElementById('button');

function buttonClick() {
  // do something
}

if (button.addEventListener) 
  button.addEventListener('click', buttonClick, false);
else
  button.attachEvent('onclick', buttonClick);
</script>

if I do it Obtrusive way:

<div id="button" onclick="buttonClick()">Button</div>
<script>
function buttonClick() {
  // do something
}
</script>

As you can see Obtrusive way is simpler and not needed to care about browser inconsistency. So could you explain or show me any examples how Unobtrusive way can help to avoid browser inconsistencies.

And third one. I can do progressive enhancement in Obtrusive way. For example, I can still use Modernizr in Obtrusive way.

How can Unobtrusive JavaScript help to do these?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

生生不灭 2024-12-06 18:14:18

嗯,你说的是真的,但是还有一些其他 HTML 属性不那么标准。例如,像

<input disabled>

or 这样

<input disabled="disabled">

的东西可以在不显眼的 javascript 中更一致地处理。

Well, what you say is true, but there are some other HTML properties that are not that standard. for example stuff like

<input disabled>

or

<input disabled="disabled">

can be handled in unobtrusive javascript more consistently.

瑶笙 2024-12-06 18:14:18

你可以使用 HTML 属性,就像 Arash 所说的那样,但它不是 W3c 有效的。
如果你要通过W3c验证,最好使用Javascript

you can use HTML properties, like Arash say, but it's not W3c Valid.
If you will pass the W3c validation, it's better to use Javascript

你的往事 2024-12-06 18:14:18

我通常更喜欢使用简单而精确的 Polyfill。
对于您的示例,您可以只包含 addEventListener polyfill (https://gist.github.com/eirikbacker/2864711< /a>) 然后使用标准方法:

<div id="button">Button</div>
<script type="text/javascript" src="./polyfills/addEventListener-polyfill.js"></script>
<script>
    var button = document.getElementById('button');

    function buttonClick() {
        // do something
    }

    button.addEventListener('click', buttonClick, false);
</script>

I generally prefer to use simple and precise polyfills.
For your example, you could just include addEventListener polyfill (https://gist.github.com/eirikbacker/2864711) and then use a standard way :

<div id="button">Button</div>
<script type="text/javascript" src="./polyfills/addEventListener-polyfill.js"></script>
<script>
    var button = document.getElementById('button');

    function buttonClick() {
        // do something
    }

    button.addEventListener('click', buttonClick, false);
</script>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文