Skip to content

Instantly share code, notes, and snippets.

@ashikawa
Created June 15, 2012 03:40
Show Gist options
  • Save ashikawa/2934564 to your computer and use it in GitHub Desktop.
Save ashikawa/2934564 to your computer and use it in GitHub Desktop.
JavaScript Babbling
<html lang="ja">
<head>
<meta charset="UTF-8">
<title>Js Babbling</title>
</head>
<body>
<section>
<div id="parent">
<a id="child" href="./page2.html">a tag</a>
</div>
</section>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
<script>
$(function () {
// TEST1 jQuery bind
var $parent = $('#parent'),
$child = $('#child');
$parent.bind('click', function (e) {
console.log('parent called!');
});
$child.bind('click', function (e) {
console.log('child1 called!');
// test1-1 preevent
e.preventDefault();
// test1-2 propagation
e.stopPropagation();
// test1-3 return false
return false;
});
$child.bind('click', function (e) {
console.log('child2 called!');
});
// TEST2 element.onclick
document.getElementById('parent').onclick = function (e) {
console.log('parent called');
};
document.getElementById('child').onclick = function (e) {
console.log('child called');
// test2-1 preevent
e.preventDefault();
// test2-2 propagation
e.stopPropagation();
// test2-3 return false
return false;
};
});
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment