Skip to content

Instantly share code, notes, and snippets.

@ituki
Created February 19, 2014 06:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ituki/9087080 to your computer and use it in GitHub Desktop.
Save ituki/9087080 to your computer and use it in GitHub Desktop.
要素のクリックイベントで一部だけ別の動作をさせたいとき ref: http://qiita.com/ituki_b/items/7fc1402df47baf332552
<p><span class="icon">ここは別の動作をさせる</span>ここは普通の動作をさせる</p>
$("p").click(function(e){
if($(e.target).hasClass("icon")) {
//.iconだけ別の動作
//attr("class")だと複数のクラスのときに動作しないので変更。
} else {
//普通の動作
}
});
<p><span id="icon">ここは別の動作をさせる</span>ここは普通の動作をさせる</p>
$("p").click(function(e){
if($(e.target).attr("id") == "icon") {
//#iconだけ別の動作
} else {
//普通の動作
}
});
<p><span>ここは別の動作をさせる</span>ここは普通の動作をさせる</p>
$("p").click(function(e){
if($(e.target).get(0).tagName == "span") {
//spanだけ別の動作
} else {
//普通の動作
}
});
$("p").click(function(e){
if($(e.target).is(要素)) {
//要素(クラスでも属性でも、要素名でも)だけ別の動作
} else {
//普通の動作
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment