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 事件会在对象被点击时发生。

请注意, onclick 与 onmousedown 不同。单击事件是在同一元素上发生了鼠标按下事件之后又发生了鼠标放开事件时才发生的。

语法

HTML 中:

<element onclick="SomeJavaScriptCode">

JavaScript 中:

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>

支持改事件的 JavaScript 对象:

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>

运行效果如下图:

持续更新,欢迎大家指教!

最新文章

  1. android 动画效果
  2. Node实践之一
  3. 遇到bug怎么办
  4. MongoDB基本命令用
  5. 转: Eclipse 分屏显示同一个文件
  6. 在Salesforce中编写Unit Test
  7. [转载]WEB缓存技术概述
  8. Java-输入输出
  9. 浅析Android的窗口
  10. CPU,MPU,MCU,SOC,SOPC联系与差别
  11. cocos2d-x学习日志(13) --A星寻路算法demo
  12. Bring it on
  13. eclipse ldt update resource
  14. ABP .Net Core 日志组件集成使用NLog
  15. 广州站长沙龙 MIP 问题及答案
  16. servlet中如何实现通过Spring实现对象的注入
  17. 1-TwoSum(简单)
  18. Java access to the Domino Objects, Part 1
  19. Codeforces Round #368 (Div. 2) B. Bakery 水题
  20. December 15th 2016 Week 51st Thursday

热门文章

  1. Websocket如何建立连接
  2. nginx--&gt;基本使用
  3. Idea maven远程调试(pom配置tomcat)
  4. python+selenium元素定位——8种方法
  5. 日记smarthome
  6. Web API 入门一
  7. Git-第一篇认识git,核心对象,常用命令
  8. HDFS启动过程概述及集群安全模式操作
  9. [常用类]StringBuffer 类,以及 StringBuilder 类
  10. 小白学Python(16)——pyecharts 绘制地理图表 Geo