在JavaScript中附加事件 [英] Attaching events in JavaScript

查看:53
本文介绍了在JavaScript中附加事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

作为对其中一个问题的评论,评论者写道(强调我的):

As comment to one of the questions here a commenter wrote (emphasis mine):


...通过使用内联onclick 你正在做类似的事情,但它更难维护,更容易出问题。 JavaScript社区作为一个整体已经暂时离开内联JavaScript。

这是指的是使用

$("#someID").click(function(){
    do something here...;
});

而不是

<a id="someID" onclick="someFunction();">

是否真的从旧学校宣布的方式转变事件内联,如果是这样,其中一个的好处是什么?

Has there really been a shift away from the old school way of declaring events inline, and if so, what are the benefits of one of the other?

编辑我想包含一个引用可能会有所帮助到原始问题。它询问是否为每个选项卡附加了不同的点击事件。 我的回答废话并且我欠FallenRayne道歉=)。

EDIT I guess it may be helpful to include a reference to the original question. It asked about attaching a different click event to each tab. Is my answer crap and do I owe FallenRayne an apology =).

推荐答案

最大的好处是内容(html)和动作/行为(javascript)的分离。这被称为不引人注目的javascript 。保持这些分开使得更容易改变而不影响另一个。

The big benefit is the separation of content (html) and action/behavior (javascript). This is known as Unobtrusive javascript. Keeping these separated makes it easier to change either without affecting the other.

这篇关于在JavaScript中附加事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆