Created
February 19, 2014 06:33
-
-
Save ituki/9087080 to your computer and use it in GitHub Desktop.
要素のクリックイベントで一部だけ別の動作をさせたいとき ref: http://qiita.com/ituki_b/items/7fc1402df47baf332552
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
<p><span class="icon">ここは別の動作をさせる</span>ここは普通の動作をさせる</p> |
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
$("p").click(function(e){ | |
if($(e.target).hasClass("icon")) { | |
//.iconだけ別の動作 | |
//attr("class")だと複数のクラスのときに動作しないので変更。 | |
} else { | |
//普通の動作 | |
} | |
}); |
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
<p><span id="icon">ここは別の動作をさせる</span>ここは普通の動作をさせる</p> |
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
$("p").click(function(e){ | |
if($(e.target).attr("id") == "icon") { | |
//#iconだけ別の動作 | |
} else { | |
//普通の動作 | |
} | |
}); |
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
<p><span>ここは別の動作をさせる</span>ここは普通の動作をさせる</p> |
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
$("p").click(function(e){ | |
if($(e.target).get(0).tagName == "span") { | |
//spanだけ別の動作 | |
} else { | |
//普通の動作 | |
} | |
}); |
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
$("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