JS 클릭 이벤트 - 뷰티 컬렉션
예
그림과 같이 결과 :
다음과 같이 코드입니다 :
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Js 美女合集</title>
<style>
* {
padding: 0;
margin: 0;
}
body{
background: #000;
}
.parent {
width: 500px;
margin: 20px auto;
}
.parent .pic {
width: 100%;
height: 400px;
}
.parent .pic img {
width: 100%;
height: 100%;
}
.box {
display: flex;
}
.box div {
flex: 1;
text-align: center;
line-height: 100px;
color: #fff;
}
.box div input {
width: 80px;
height: 30px;
border-radius: 5px;
}
#txt{
color: red;
font-size: 24px;
}
</style>
</head>
<body>
<div class="parent">
<div class="pic">
<img src="1.jpg" alt="" id="pic">
</div>
<div class="box">
<div>
<input type="button" value="上一张" id="btnLast">
</div>
<div>
第 <span id="txt">1</span> 张
</div>
<div>
<input type="button" value="下一张" id="btnNext">
</div>
</div>
</div>
<script>
var index = 1;
document.getElementById("btnNext").onclick = function () {
if (index < 14) {
index++;
}
document.getElementById("txt").innerHTML=index;
document.getElementById("pic").src = index + ".jpg";
};
document.getElementById("btnLast").onclick = function () {
if (index > 1) {
index--;
}
document.getElementById("txt").innerHTML=index;
document.getElementById("pic").src = index + ".jpg";
};
</script>
</body>
</html>
정의 및 사용
개체를 클릭 할 때 온 클릭 이벤트가 발생합니다.
제발 참고, onclick을 다른하면 onMouseDown. 마우스가 마우스 이벤트가 열려 행사에 일어났다 누른 후 같은 요소에있을 때 Click 이벤트가 발생합니다.
문법
HTML에서 :
<element onclick="SomeJavaScriptCode">
자바 스크립트 中 :
object.onclick=function(){SomeJavaScriptCode};
HTML 태그는 이벤트를 지원합니다
<a>, <address>, <area>, <b>, <bdo>, <big>, <blockquote>, <body>, <button>,
<caption>, <cite>, <code>, <dd>, <dfn>, <div>, <dl>, <dt>, <em>, <fieldset>,
<form>, <h1> to <h6>, <hr>, <i>, <img>, <input>, <kbd>, <label>, <legend>,
<li>, <map>, <object>, <ol>, <p>, <pre>, <samp>, <select>, <small>, <span>,
<strong>, <sub>, <sup>, <table>, <tbody>, <td>, <textarea>, <tfoot>, <th>,
<thead>, <tr>, <tt>, <ul>, <var>
지원 자바 스크립트는 변경 이벤트 객체 :
button, document, checkbox, link, radio, reset, submit
예
다음과 같이 코드입니다 :
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Js 美女合集</title>
<style>
* {
padding: 0;
margin: 0;
}
body{
background: #000;
}
.parent {
width: 500px;
margin: 20px auto;
}
.parent .pic {
width: 100%;
height: 400px;
}
.parent .pic img {
width: 100%;
height: 100%;
}
.box {
display: flex;
}
.box div {
flex: 1;
text-align: center;
line-height: 100px;
color: #fff;
}
.box div input {
width: 80px;
height: 30px;
border-radius: 5px;
}
#txt{
color: red;
font-size: 24px;
}
</style>
</head>
<body>
<div class="parent">
<div class="pic">
<img src="1.jpg" alt="" id="pic">
</div>
<div class="box">
<div>
<input type="button" value="上一张" id="btnLast">
</div>
<div>
第 <span id="txt">1</span> 张
</div>
<div>
<input type="button" value="下一张" id="btnNext">
</div>
</div>
</div>
<script>
var index = 1;
var txt=document.getElementById("txt");
var pic= document.getElementById("pic");
document.getElementById("btnNext").onclick = function () {
if (index < 14) {
index++;
}
txt.innerHTML=index;
document.getElementById("pic").src = index + ".jpg";
};
document.getElementById("btnLast").onclick = function () {
if (index > 1) {
index--;
}
txt.innerHTML=index;
pic.src = index + ".jpg";
};
</script>
</body>
</html>
다음과 같이도 결과를 작동 :