慕课网js触发事件系列demo

写在前面:在学js,顺便做了下慕课网上面事件系列的课程,将每个事件都一一做了demo,我也为了加深印象,把demo都放在这里。有兴趣的童鞋,可以复制回去试试看。

js鼠标单击事件( onclick )

onclick是鼠标单击事件,当在网页上单击鼠标时,就会发生该事件。同时onclick事件调用的程序块就会被执行,通常与按钮一起使用。
下面是代码:

<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>单击事件 </title>
<script type="text/javascript">
  function openwin(){   window.open('http://www.imooc.com','_blank','height=600,width=400,top=100,toolbar=no,left=0,menubar=no,scrollbars=no,status=no');}
</script>
</head>
<body>
  <form>
    <input name="点击我" type="button" value="点击我" onclick="openwin()"/>
  </form>
</body>
</html>

鼠标经过事件(onmouseover)

鼠标经过事件,当鼠标移到一个对象上时,该对象就触发onmouseover事件,并执行onmouseover事件调用的程序。

<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title> 鼠标经过事件 </title>
<script type="text/javascript">
    function message(){
      confirm("请输入密码后,再单击确定!"); }
</script>
</head>
<body>
<form>
密码:<input name="password" type="password" >
<input name="确定" type="button" value="确定"/>
</form>
</body>
</html>

鼠标移开事件(onmouseout)

鼠标移开事件,当鼠标移开当前对象时,执行onmouseout调用的程序。

<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>鼠标移开事件 </title>
<script type="text/javascript">
  function message(){
    alert("不要移开,点击后进行慕课网!"); }
</script>
</head>
<body>
<form onmouseout="message()">
  <a href="http://www.imooc.com">点击我</a>
</form>
</body>
</html>

光标聚焦事件(onfocus)
当网页中的对象获得聚点时,执行onfocus调用的程序就会被执行。

<!DOCTYPE HTML>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title> 光标聚焦事件 </title>
  <script type="text/javascript">
    function message(){
     var new2=document.getElementById("new1");
     new2.style.backgroundColor="pink";
    }
    function message1(){
        var new3=document.getElementById("new1");
       // new3.style.backgroundColor="purple";
       new3.removeAttribute("style");
    }
  </script>
</head>
<body>
请选择您的职业:<br>
  <form>
    <select name="career" onfocus="message()" onblur="message1()" id="new1"> 
      <option>学生</option> 
      <option>教师</option> 
      <option>工程师</option> 
      <option>演员</option> 
      <option>会计</option> 
    </select> 
  </form>
</body>
</html>

失焦事件(onblur)
onblur事件与onfocus是相对事件,当光标离开当前获得聚焦对象的时候,触发onblur事件,同时执行被调用的程序。

<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title> 失焦事件 </title>
<script type="text/javascript">
  function message(){
    alert("请确定已输入密码后,在移开!"); }
</script>    
</head>
<body>
  <form>
   用户:<input name="username" type="text" value="请输入用户名!"  onblur="message()">
   密码:<input name="password" type="text" value="请输入密码!" >
  </form>
</body>
</html>

内容选中事件(onselect)
选中事件,当文本框或者文本域中的文字被选中时,触发onselect事件,同时调用的程序就会被执行。

<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title> 内容选中事件 </title>
<script type="text/javascript">
  function message(){
    alert("您触发了选中事件!"); }
</script>    
</head>
<body>
  <form>
  个人简介:<br>
   <textarea name="summary" cols="60" rows="5" onselect="message()">请写入个人简介,不少于200字!左键选中文字可触发事件</textarea>
  </form>
</body>
</html>

文本框内容改变事件(onchange)
通过改变文本框的内容来触发onchange事件,同时执行被调用的程序。

<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title> 文本框内容改变事件 </title>
<script type="text/javascript">
  function message(){
    alert("您改变了文本内容!"); }
</script>    
</head>
<body>
  <form>
  个人简介:<br>
   <textarea name="summary" cols="60" rows="5" onchange="message()">请写入个人简介,不少于200字!</textarea>
  </form>
</body>
</html>

加载事件(onload)
事件会在页面加载完成后,立即发生,同时执行被调用的程序。
注意:1. 加载页面时,触发onload事件,事件写在<body>标签内。
2.此节的加载页面,可理解为打开一个新页面时。如下代码,当加载一个新页面时,弹出对话框“加载中,请稍等…”

<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title> 加载事件 </title>
<script type="text/javascript">
  function message(){
    alert("加载中,请稍等…"); }
</script>    
</head>
<body onload="message()">
  欢迎学习JavaScript。
</body>
</html>

卸载事件(onunload)
当用户退出页面时(页面关闭、页面刷新等),触发onUnload事件,同时执行被调用的程序。
这里我直接放慕课网的网址了,我demo做不出来:http://www.imooc.com/code/675

最后一题:


Paste_Image.png

最后又到了观众朋友们最喜欢的求赞求关注环节:希望看完的朋友点个喜欢,想关注我这个菜鸡是如何成长的也可以关注一下我,基本上每个月都不会少于十五篇文章(看到干货我也会进行分享)。然后github也互相加个star。码字不易,感谢支持,感激不尽!

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是一个 Vue 滚动区域滚动到底部触发事件并且节流的 demo: ```vue <template> <div class="scroll-area" ref="scrollArea" @scroll="handleScroll"> <!-- 滚动区域内容 --> </div> </template> <script> export default { data() { return { throttleTimer: null, throttleInterval: 300, // 节流时间间隔 }; }, methods: { handleScroll() { // 滚动区域距离顶部的高度 const scrollTop = this.$refs.scrollArea.scrollTop; // 滚动区域的总高度 const scrollHeight = this.$refs.scrollArea.scrollHeight; // 滚动区域的可见高度 const clientHeight = this.$refs.scrollArea.clientHeight; // 判断是否滚动到底部 if (scrollTop + clientHeight >= scrollHeight) { this.throttle(this.handleScrollToBottom); } }, handleScrollToBottom() { // 滚动到底部后要执行的操作 }, throttle(func) { if (!this.throttleTimer) { this.throttleTimer = setTimeout(() => { this.throttleTimer = null; func(); }, this.throttleInterval); } }, }, }; </script> ``` 在这个 demo 中,我们使用了 `@scroll` 事件来监听滚动区域的滚动事件。在 `handleScroll` 方法中,我们获取了滚动区域距离顶部的高度、滚动区域的总高度和滚动区域的可见高度,并判断是否滚动到底部。如果滚动到底部,则调用 `throttle` 方法来执行 `handleScrollToBottom` 方法。 `throttle` 方法是一个简单的节流函数,它会在一定的时间间隔内只执行一次传入的函数。在这个 demo 中,我们使用了 `this.throttleInterval` 来定义节流的时间间隔。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值