Skip to content

Instantly share code, notes, and snippets.

@niku
Created March 27, 2014 05:32
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 niku/9800972 to your computer and use it in GitHub Desktop.
Save niku/9800972 to your computer and use it in GitHub Desktop.
<!DOCTYPE html>
<html lang="ja">
<head>
<meta charset="UTF-8">
<script src="http://code.jquery.com/jquery-1.11.0.min.js"></script>
<title>jQuery test</title>
<style type="text/css">
p { background-color: #afc; padding: .5em }
</style>
</head>
<body>
<p id="elem1">要素 1</p>
<p id="elem2">要素 2</p>
<p id="elem3">要素 3</p>
<p id="elem4">要素 4</p>
<p id="elem5">要素 5</p>
<p id="elem6">要素 6</p>
<p id="elem7">要素 7</p>
<p id="elem8">要素 8</p>
<p id="elem9">要素 9</p>
<p id="elem10">要素 10</p>
</body>
<script type="text/javascript">
$(document).ready(function() {
var extractNumber = function(id) {
var matcher = /^elem(\d+)$/;
return id.match(matcher)[1];
};
$('p').bind('click', function() {
var number = extractNumber(this.id);
alert(number);
});
});
</script>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment