戻る

メソッドチェーン

イベントハンドラ(body要素に記述)

<!DOCTYPE html>
<html lang="ja-JP">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width,user-scalable=yes,initial-scale=1.0,minimum-scale=1.0,maximum-scale=1.5">
<title></title>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
<script>
function changeImage(){
	document.getElementsByTagName("img")[0].style.width = "400px";
	document.getElementsByTagName("img")[0].style.height = "400px";
	document.getElementsByTagName("img")[0].setAttribute("src","img/after.png");
}
</script>
</head>
<body>
	<img src="img/before.png">
	<input type="button" value="click" onclick="changeImage();">
</body>
</html>

イベントハンドラ(scriptタグ内に記述)

<!DOCTYPE html>
<html lang="ja-JP">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width,user-scalable=yes,initial-scale=1.0,minimum-scale=1.0,maximum-scale=1.5">
<title></title>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
<script>
window.onload = function(){
	document.getElementsByTagName("input")[0].onclick = function(){
		document.getElementsByTagName("img")[0].style.width = "400px";
		document.getElementsByTagName("img")[0].style.height = "400px";
		document.getElementsByTagName("img")[0].setAttribute("src","img/after.png");
	}
}
</script>
</head>
<body>
	<img src="img/before.png">
	<input type="button" value="click">
</body>
</html>

イベントリスナ

<!DOCTYPE html>
<html lang="ja-JP">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width,user-scalable=yes,initial-scale=1.0,minimum-scale=1.0,maximum-scale=1.5">
<title></title>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
<script>
window.addEventListener("load",function(){
	document.getElementsByTagName("input")[0].addEventListener("click",function(){
		document.getElementsByTagName("img")[0].style.width = "400px";
		document.getElementsByTagName("img")[0].style.height = "400px";
		document.getElementsByTagName("img")[0].setAttribute("src","img/after.png");
	});
});
</script>
</head>
<body>
	<img src="img/before.png">
	<input type="button" value="click">
</body>
</html>

jQueryのメソッドチェーン

<!DOCTYPE html>
<html lang="ja-JP">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width,user-scalable=yes,initial-scale=1.0,minimum-scale=1.0,maximum-scale=1.5">
<title></title>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
<script>
$(function(){
	$("input").click(function(){
		$("img")
		.attr("src","img/after.png")
		.css({
			width:"400px",
			height:"400px"
		})
	});
});
</script>
</head>
<body>
	<img src="img/before.png">
	<input type="button" value="click">
</body>
</html>

inserted by FC2 system