案例一.点击按钮,选中input中的全部内容

select()方法:选中全部。

点击按钮选中输入框中的内容!!!!

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<style>
input,
button{
margin: 0;
padding: 0;
outline: none;
}
input{
width:200px;
height:40px;
border:1px solid black;
font-size: 20px;
}
button{
width: 300px;
line-height: 40px;
vertical-align: top;
}
</style>
</head>
<body>
<input type="text">
<button>点击此按钮选中input的全部内容</button>
<script>
var input=document.getElementsByTagName("input")[0];
var button=document.getElementsByTagName("button")[0];
button.onclick=function () {
input.select();
}
</script>
</body>
</html>

案例二.点击按钮,选中input中的全部内容,并复制到粘贴板上。

document.execCommand("copy");//复制到粘贴板上。

点击按钮,选中并复制到粘贴板上,点击查看效果以及代码

<body>
<input type="text">
<button>点击此按钮选中input的全部内容</button>
<script>
var input=document.getElementsByTagName("input")[0];
var button=document.getElementsByTagName("button")[0];
button.onclick=function () {
input.select();//选中input的所有内容
document.execCommand("copy");//复制到粘贴板上
}
</script>
</body>

事件对象

事件:当用户对页面进行操作的交互时,会触发对应元素的事件。
事件对象:

        event
当发生事件,执行事件处理函数的时候,该时刻的详细信息。
注意:如果函数是直接调用的,则没有事件对象
注意:不同事件中的event对象可能有不同

举例说明:

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title></title>
<style>
#box{
width:100px;
height:100px;
background-color:red;
}
</style>
</head>
<body>
<div id="box"></div>
<script>
var box = document.getElementById("box");
// box.onmouseover = fn;//打印undefined
// box.onmousedown = fn;//打印undefined
document.onkeydown = fn;//打印按下的键值
// fn();//报错
function fn(){
// console.log( typeof event );
console.log( event.keyCode );
}
</script> </body>
</html>

案例三:模拟苹果电脑菜单

点击查看效果以及代码!!!!

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<style>
body{
margin: 0;
padding: 0;
}
.wrap{
border:1px solid black;
position: absolute;
bottom:100px;
text-align: center;
width: 100%;
}
.wrap div{
width: 100px;
height: 100px;
background: cornflowerblue;
display: inline-block;
vertical-align: bottom;
}
</style>
</head>
<body>
<div class="wrap">
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
</div>
<script>
var wrap=document.getElementsByClassName("wrap")[0];
var divs=wrap.getElementsByTagName("div");
document.onmousemove=function () {
for(var i=0;i<divs.length;i++){
calc( divs[i],event);
}
}
function calc( obj,mouseEvent ){
var objPos = {
//offsetLeft:相对于最近定位父级定位元素
x: obj.offsetLeft + 50,
//obj.getBoundingClientRect().top:距离文档顶部位置
y: obj.getBoundingClientRect().top + 50
}//元素中心点坐标
var mosPos = {
x: mouseEvent.clientX,
y: mouseEvent.clientY
}//鼠标所在位置
var dis = Math.sqrt( Math.pow( objPos.x-mosPos.x,2 ) + Math.pow( objPos.y-mosPos.y,2 ) );
var scale = 1;
//当鼠标离原点中心距离小于200时,则放大
if( dis < 200 ){
scale = (200 - dis) / 200 + 1;
}
obj.style.width = 100 * scale + "px";
obj.style.height = 100 * scale + "px";
}
</script>
</body>
</html>

最新文章

  1. H3C ipsec ike 协商配置
  2. Android 渗透测试学习手册 翻译完成!
  3. Handler 原理分析和使用(二)
  4. JSON互转
  5. (Qt 翻译) QGLSceneNode
  6. 为代码减负之&amp;lt;一&amp;gt;触发器(SQL)
  7. JavaScript 作用域 匿名函数 模仿块级作用域(私有作用域)
  8. GSS4 - Can you answer these queries IV(线段树懒操作)
  9. ASP VNext 开源服务容错处理库Polly使用文档
  10. 开始在web中使用JS Modules
  11. fine-tuning 两阶段模型
  12. Wannafly挑战赛23 T2游戏 SG函数
  13. DApp demo之pet-shop
  14. python笔记12-字典
  15. GuavaCache学习笔记三:底层源码阅读
  16. linux 下设置定时任务
  17. 逻辑回归(logic regression)的分类梯度下降
  18. JavaScript 字符串用于存储和处理文本
  19. 并发修改异常(ConcurrentModificationException)
  20. SQL Server 学习博客分享列表(应用式学习 + 深入理解)

热门文章

  1. D. Colored Boots(STL)
  2. bzoj 3450 Tyvj1952 Easy (概率dp)
  3. BucketSort(桶排序)原理及C++代码实现
  4. Qt error C2338: No Q_OBJECT in the class with the signal错误解决办法(无法编译过信号与槽)
  5. python语法基础-面向对象-进阶-长期维护
  6. 堆优DIJ模板
  7. [LC] 221. Maximal Square
  8. [LC] 64. Minimum Path Sum
  9. SHELL小练习
  10. 赫夫曼解码(day17)