Created
May 30, 2013 10:02
-
-
Save kenji0x02/5676888 to your computer and use it in GitHub Desktop.
javascriptでonclickイベントを取得して、id名とかクラス名とか属性を引数にして処理する。jQueryを使うべし。
(onClick="hoge('id')"とかにすると、id以外の情報(クラス名とか属性とか)をとれないし、htmlがださくなるw)
onClickとjQueryのclickを併用したりなんかすると、なんだかわけがわからなくなるのでどっちかで笑
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Don't use: onClick="hoge('id')" | |
$('.btn').click(function(){ | |
var id = $(this).attr('id'); | |
var class = $(this).attr('class'); | |
var dataHoge = $(this).attr('data-hoge'); | |
hogeId(id); | |
hogeClass(class); | |
hogeDataHoge(dataHoge); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
DOMのイベントハンドリングはBackbone.jsのViewで行うのがいい。
jQueryやaddEventListnerを使うとコードが分散しがちになる。
(「backbone.jsガイドブック」より)
たしかにw