事件类型
document. addEventListener ( 'contextmenu' , function ( e) {
e. preventDefault ( ) ;
} )
document. addEventListener ( 'selectstart' , function ( e) {
e. preventDefault ( ) ;
} )
移动端触摸事件
移动端触摸事件分析
< ! 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> Document< / title>
< style>
div {
width: 100 px;
height: 100 px;
background- color: pink;
}
< / style>
< / head>
< body>
< div> < / div>
< script>
var div = document. querySelector ( 'div' ) ;
div. addEventListener ( 'touchstart' , function ( ) {
console. log ( '我摸了你' ) ;
} ) ;
div. addEventListener ( 'touchmove' , function ( ) {
console. log ( '我继续摸' ) ;
} ) ;
div. addEventListener ( 'touchend' , function ( ) {
console. log ( '轻轻的我走了' ) ;
} ) ;
< / script>
< / body>
< / html>
< ! 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> Document< / title>
< style>
div {
width: 100 px;
height: 100 px;
background- color: pink;
}
< / style>
< / head>
< body>
< div> < / div>
< script>
var div = document. querySelector ( 'div' ) ;
div. addEventListener ( 'touchstart' , function ( e) {
console. log ( e. targetTouches[ 0 ] ) ;
} ) ;
div. addEventListener ( 'touchmove' , function ( ) {
} ) ;
div. addEventListener ( 'touchend' , function ( e) {
} ) ;
< / script>
< / body>
< / html>
移动端案例事件分析(拖拽元素)
< ! 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> Document< / title>
< style>
div {
position: absolute;
left: 0 ;
width: 100 px;
height: 100 px;
background- color: pink;
}
< / style>
< / head>
< body>
< div> < / div>
< script>
var div = document. querySelector ( 'div' ) ;
var startX = 0 ;
var startY = 0 ;
var x = 0 ;
var y = 0 ;
div. addEventListener ( 'touchstart' , function ( e) {
startX = e. targetTouches[ 0 ] . pageX;
startY = e. targetTouches[ 0 ] . pageY;
x = this . offsetLeft;
y = this . offsetTop;
} ) ;
div. addEventListener ( 'touchmove' , function ( e) {
var moveX = e. targetTouches[ 0 ] . pageX - startX;
var moveY = e. targetTouches[ 0 ] . pageY - startY;
this . style. left = x + moveX + 'px' ;
this . style. top = y + moveY + 'px' ;
e. preventDefault ( ) ;
} ) ;
< / script>
< / body>
< / html>
任意表格案例
< head>
< meta charset= "UTF-8" >
< meta name= "viewport" content= "width=device-width, initial-scale=1.0" >
< title> Document< / title>
< style>
table {
width: 100 % ;
border: 1 px solid red;
border- spacing: 0 px;
border- collapse: collapse;
}
table tr td {
border: 1 px solid red;
}
< / style>
< / head>
< body>
行:< input type= "text" id= "num1" >
列: < input type= "text" id= "num2" >
< button id= "btn" > 生成< / button>
< input type= "checkbox" checked>
< table id= "table" >
< ! --
< tr>
< td> < / td>
< td> < / td>
< / tr>
< tr>
< td> < / td>
< td> < / td>
< / tr>
-- >
< / table>
< script>
btn. onclick = function ( ) {
var n1 = num1. value;
var n2 = num2. value;
var res = '' ;
for ( var i = 0 ; i < n1; i++ ) {
res += '<tr>'
for ( var j = 0 ; j < n2; j++ ) {
res += '<td>' + j + '</td>'
}
res += '</tr>'
}
table. innerHTML = res;
}
< / script>
< / body>
反选和全选和不选
< head>
< meta charset= "UTF-8" >
< meta name= "viewport" content= "width=device-width, initial-scale=1.0" >
< title> Document< / title>
< / head>
< body>
< button id= "btn1" > 全选< / button>
< button id= "btn2" > 不选< / button>
< button id= "btn3" > 反选< / button>
< div>
< input type= "checkbox" >
< input type= "checkbox" >
< input type= "checkbox" >
< input type= "checkbox" >
< input type= "checkbox" >
< input type= "checkbox" >
< input type= "checkbox" >
< input type= "checkbox" >
< input type= "checkbox" >
< input type= "checkbox" >
< / div>
< script>
var btn = document. getElementsByTagName ( 'button' ) ;
var inputs = document. getElementsByTagName ( 'input' ) ;
for ( var i = 0 ; i < btn. length; i++ ) {
btn[ i] . onclick = function ( ) {
if ( this . innerHTML === '全选' ) {
for ( var j = 0 ; j < inputs. length; j++ ) {
inputs[ j] . checked = true ;
}
} else if ( this . innerHTML === '反选' ) {
for ( var j = 0 ; j < inputs. length; j++ ) {
inputs[ j] . checked = ! inputs[ j] . checked;
}
} else if ( this . innerHTML === '不选' ) {
for ( var j = 0 ; j < inputs. length; j++ ) {
inputs[ j] . checked = false ;
}
}
}
}
< / script>
点击按钮实现选项卡不同
< head>
< meta charset= "UTF-8" >
< meta name= "viewport" content= "width=device-width, initial-scale=1.0" >
< title> Document< / title>
< style>
. box {
margin: 100 px;
width: 460 px;
}
. box . btn {
height: 40 px;
margin- bottom: 5 px;
}
. box . btn input {
float: left;
width: 100 px;
text- align: center;
height: 40 px;
margin- right: 20 px;
border: 1 px solid #334455 ;
}
. box . btn input: nth- child ( 4 ) {
margin- right: 0 px;
}
. box . con {
width: 460 px;
height: 400 px;
border: 1 px solid #ccc;
box- sizing: border- box;
display: none;
}
. box . btn . active {
background: orange;
}
< / style>
< / head>
< body>
< div class = "box" >
< div class = "btn" >
< input type= "button" class = "btnList active" value= "体育" >
< input type= "button" class = "btnList" value= "娱乐" >
< input type= "button" class = "btnList" value= "新闻" >
< input type= "button" class = "btnList" value= "教育" >
< / div>
< div class = "con" style= "display: block;" > 体育< / div>
< div class = "con" > 娱乐< / div>
< div class = "con" > 新闻< / div>
< div class = "con" > 教育< / div>
< / div>
< script>
var btn = document. getElementsByTagName ( 'input' ) ;
var con = document. getElementsByClassName ( 'con' ) ;
for ( var i = 0 ; i < btn. length; i++ ) {
btn[ i] . idx = i;
btn[ i] . onclick = function ( ) {
for ( var j = 0 ; j < btn. length; j++ ) {
btn[ j] . className = 'btnList' ;
con[ j] . style. display = 'none' ;
}
this . className = 'btnList active' ;
con[ this . idx] . style. display = 'block' ;
}
}
< / script>
< / body>
随机给body生成背景色
< head>
< meta charset= "UTF-8" >
< meta name= "viewport" content= "width=device-width, initial-scale=1.0" >
< title> Document< / title>
< style>
body {
background: pink;
}
< / style>
< / head>
< body>
< button id= "btn" > 点击换色< / button>
< script src= "js/utils.js" > < / script>
< script>
btn. onclick = function ( ) {
document. body. style. background = 'rgb(' + randomNum ( 0 , 255 ) + ',' + randomNum ( 0 , 255 ) + ',' + randomNum ( 0 , 255 ) + ')' ;
}
< / script>
< / body>
简易验证码-验证信息
< head>
< meta charset= "UTF-8" >
< meta name= "viewport" content= "width=device-width, initial-scale=1.0" >
< title> Document< / title>
< style>
span {
display: inline- block;
width: 80 px;
height: 30 px;
background: #ccc;
text- align: center;
line- height: 30 px;
user- select: none;
}
< / style>
< / head>
< body>
< input type= "text" id= "num" >
< span id= "code" > < / span>
< button id= "btn" > 验证< / button>
< script src= "js/utils.js" > < / script>
< script>
code. innerHTML = randomNum ( 1000 , 9999 ) ;
btn. onclick = function ( ) {
if ( num. value === code. innerHTML) {
alert ( '验证成功' )
} else {
alert ( '验证失败' ) ;
code. innerHTML = randomNum ( 1000 , 9999 ) ;
num. value = '' ;
}
}
code. onclick = function ( ) {
this . innerHTML = randomNum ( 1000 , 9999 ) ;
}
< / script>
< / body>
封装十六进制随机颜色
< script>
function randomNum ( n, m) {
var max, min;
max = n > m ? n : m;
min = n < m ? n : m;
return parseInt ( Math. random ( ) * ( max - min + 1 ) + min) ;
}
var str = '0123456789abcedf' ;
var color = '#' ;
for ( var i = 0 ; i < 6 ; i++ ) {
color += str[ randomNum ( 0 , 15 ) ]
}
document. body. style. background = color;
< / script>
简易日历
< head>
< meta charset= "UTF-8" >
< meta name= "viewport" content= "width=device-width, initial-scale=1.0" >
< title> Document< / title>
< style type= "text/css" >
* {
padding: 0 ;
margin: 0 ;
}
li {
list- style: none;
}
body {
background: #f6f9fc;
font- family: arial;
}
. calendar {
width: 210 px;
margin: 0 auto;
padding: 10 px 10 px 20 px 20 px;
background: #eae9e9;
}
. calendar ul {
width: 210 px;
overflow: hidden;
padding- bottom: 10 px;
}
. calendar li {
float: left;
width: 58 px;
height: 54 px;
margin: 10 px 10 px 0 0 ;
border: 1 px solid #fff;
background: #424242 ;
color: #fff;
text- align: center;
cursor: pointer;
}
. calendar li h2 {
font- size: 20 px;
padding- top: 5 px;
}
. calendar li p {
font- size: 14 px;
}
. calendar . active {
border: 1 px solid #424242 ;
background: #fff;
color: #e84a7e;
}
. calendar . active p {
font- weight: bold;
}
. calendar . text {
width: 178 px;
padding: 0 10 px 10 px;
border: 1 px solid #fff;
padding- top: 10 px;
background: #f1f1f1;
color: #555 ;
}
. calendar . text h2 {
font- size: 14 px;
margin- bottom: 10 px;
}
. calendar . text p {
font- size: 12 px;
line- height: 18 px;
}
< / style>
< / head>
< body>
< div id= "tab" class = "calendar" >
< ul id= "ul" >
< li class = "active" >
< h2> 1 < / h2>
< p> JAN < / p>
< / li>
< li>
< h2> 2 < / h2>
< p> FER < / p>
< / li>
< li>
< h2> 3 < / h2>
< p> MAR < / p>
< / li>
< li>
< h2> 4 < / h2>
< p> APR < / p>
< / li>
< li>
< h2> 5 < / h2>
< p> MAY < / p>
< / li>
< li>
< h2> 6 < / h2>
< p> JUN < / p>
< / li>
< li>
< h2> 7 < / h2>
< p> JUL < / p>
< / li>
< li>
< h2> 8 < / h2>
< p> AUG < / p>
< / li>
< li>
< h2> 9 < / h2>
< p> SEP < / p>
< / li>
< li>
< h2> 10 < / h2>
< p> OCT < / p>
< / li>
< li>
< h2> 11 < / h2>
< p> NOV < / p>
< / li>
< li>
< h2> 12 < / h2>
< p> DEC < / p>
< / li>
< / ul>
< div class = "text" id= "txt" >
< h2 id= "month" > 1 月活动< / h2>
< p id= "title" > 快过年了,大家可以商量着去哪玩吧~< / p>
< / div>
< / div>
< script>
var lis = document. getElementsByTagName ( 'li' ) ;
var arr = [
'1月份活动的数据是好好过年' ,
'2月份活动的数据是好好过年' ,
'3月份活动的数据是好好过年' ,
'4月份活动的数据是好好过年' ,
'5月份活动的数据是好好过年' ,
'6月份活动的数据是好好过年' ,
'7月份活动的数据是好好过年' ,
'8月份活动的数据是好好过年' ,
'9月份活动的数据是好好过年' ,
'10月份活动的数据是好好过年' ,
'11月份活动的数据是好好过年' ,
'12月份活动的数据是好好过年'
]
for ( var i = 0 ; i < lis. length; i++ ) {
lis[ i] . index = i;
lis[ i] . onclick = function ( ) {
for ( var j = 0 ; j < lis. length; j++ ) {
lis[ j] . className = '' ;
}
this . className = 'active' ;
month. innerHTML = this . index + 1 + '月份活动'
title. innerHTML = arr[ this . index] ;
}
}
< / script>
< / body>
模拟快递单号的查询
< ! 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> Document< / title>
< style>
* {
margin: 0 ;
padding: 0 ;
}
. search {
position: relative;
width: 178 px;
margin: 100 px;
}
. con {
display: none;
position: absolute;
top: - 40 px;
width: 171 px;
border: 1 px solid rgba ( 0 , 0 , 0 , .2 ) ;
box- shadow: 0 2 px 4 px rgba ( 0 , 0 , 0 , .2 ) ;
padding: 5 px 0 ;
font- size: 18 px;
line- height: 20 px;
color: #333 ;
}
. con: : before {
content: '' ;
width: 0 ;
height: 0 ;
position: absolute;
top: 28 px;
left: 18 px;
border: 8 px solid #000 ;
border- style: solid dashed dashed;
border- color: #fff transparent transparent;
}
< / style>
< / head>
< body>
< div class = "search" >
< div class = "con" > 123 < / div>
< input type= "text" placeholder= "请输入您的快递单号" class = "jd" >
< / div>
< script>
var con = document. querySelector ( '.con' ) ;
var jd_input = document. querySelector ( '.jd' ) ;
jd_input. addEventListener ( 'keyup' , function ( ) {
if ( this . value == '' ) {
con. style. display = 'none' ;
} else {
con. style. display = 'block' ;
con. innerText = this . value;
}
} )
jd_input. addEventListener ( 'blur' , function ( ) {
con. style. display = 'none' ;
} )
jd_input. addEventListener ( 'focus' , function ( ) {
if ( this . value !== '' ) {
con. style. display = 'block' ;
}
} )
< / script>
< / body>