jQueryの処理について質問です
jQuery(function($) { $(’.hoge’).bind(’mousedown’, function() { 処理 }); $(’’.hoge’).bind(’touchstart’, function() { 処理 });});
と、現状、同じ処理なのにfunctionを分けて書いています。mousedownとtouchstartを一緒にして、処理を分けずに書く方法をどなたか教えていただけないでしょうか。
jQuery(function($) { $(’.hoge’). bind(’mousedown’, function() { 処理 }). bind(’touchstart’, function() { 処理 });});
※見やすさのために全角スペース入れてますので、コピペ時は注意※改行位置は変わっても大丈夫です
あ、「処理」の内容が同じなら、下記でもいいです。
jQuery(function($) { $(’.hoge’). bind(’mousedown touchstart’, function() { 処理 });});
2回もbindしなくていいよ
jQuery(function($) { $(’.hoge’).bind({ ’mousedown’: function() { console.log(’mousedown’); }, ’mouseup’: function() { console.log(’mouseup’); } });});
うまくいきました!ありがとうございます!
あ、トピ主です。
マジレスするとjQueryのbindは非推薦なので、onを使おう。
バージョンがもしかしたら古いのかも!
なるほど!
みんなの回答 2 件
jQuery(function($) {
$(’.hoge’).
bind(’mousedown’, function() {
処理
}).
bind(’touchstart’, function() {
処理
});
});
※見やすさのために全角スペース入れてますので、コピペ時は注意
※改行位置は変わっても大丈夫です
マジレスするとjQueryのbindは非推薦なので、onを使おう。
jQueryのbind/live/delegateの違いまとめ、と新API .on()の使い方 - y-kawazの日記
http://d.hatena.ne.jp/y-kawaz/20111002/1317489435関連するトピックス