当前位置: 首页 > news >正文

网站建设 广西江苏seo

网站建设 广西,江苏seo,引流平台有哪些,企业网站建设规划的基本原则假设我们有一个按钮,用户点击该按钮后,会选择一个文件,且我们希望每次点击按钮时只触发一次文件处理。下面我会给你一个简单的例子,展示放在函数内部和放在函数外部的区别。 1. 将事件监听器放在函数内部(问题的根源&…

假设我们有一个按钮,用户点击该按钮后,会选择一个文件,且我们希望每次点击按钮时只触发一次文件处理。下面我会给你一个简单的例子,展示放在函数内部和放在函数外部的区别。

1. 将事件监听器放在函数内部(问题的根源)

<!DOCTYPE html>
<html lang="en">
<head><meta charset="UTF-8"><meta name="viewport" content="width=device-width, initial-scale=1.0"><title>事件监听器放在函数内部</title>
</head>
<body><button onclick="onclickbtnLoadMainLVDS()">点击选择文件</button><input type="file" id="id_file_loadLVDS" style="display:none"><script>function onclickbtnLoadMainLVDS() {document.getElementById('id_file_loadLVDS').click();  // 点击按钮时触发文件选择const fileInput = document.getElementById('id_file_loadLVDS');// 每次点击按钮时,都给文件输入框绑定事件监听器fileInput.addEventListener('change', function(e) {console.log('文件被选择了');});}</script>
</body>
</html>

问题:

  • 在这个例子中,每次点击按钮时,我们都会调用 onclickbtnLoadMainLVDS 函数。
  • 每次函数调用时,都为 input 元素绑定了一个新的 change 事件监听器。
  • 如果你点击按钮 多次,那么就会为同一个文件输入框绑定 多个监听器
  • 这样,当用户选择文件时,事件会触发 多个监听器,导致相同的事件处理逻辑执行多次。

例如,点击按钮两次会绑定两个监听器,再选择文件时,控制台会输出两次 "文件被选择了"

2. 将事件监听器放在函数外部(解决问题)

<!DOCTYPE html>
<html lang="en">
<head><meta charset="UTF-8"><meta name="viewport" content="width=device-width, initial-scale=1.0"><title>事件监听器放在函数外部</title>
</head>
<body><button onclick="onclickbtnLoadMainLVDS()">点击选择文件</button><input type="file" id="id_file_loadLVDS" style="display:none"><script>// 在函数外部绑定一次事件监听器const fileInput = document.getElementById('id_file_loadLVDS');fileInput.addEventListener('change', function(e) {console.log('文件被选择了');});function onclickbtnLoadMainLVDS() {document.getElementById('id_file_loadLVDS').click();  // 点击按钮时触发文件选择}</script>
</body>
</html>

解决问题的方式:

  • 在这个例子中,我们 只在页面加载时 就绑定了一个 change 事件监听器。
  • 无论按钮点击多少次,事件监听器始终只会绑定一次。
  • 这样,每次用户选择文件时,事件只会触发 一次,无论按钮点击多少次。

总结

  • 函数内部绑定事件监听器的坏处:每次点击按钮时都重新绑定事件监听器,导致事件处理程序被多次调用。如果你点击按钮很多次,事件监听器会被重复绑定,最终导致每次文件选择触发多个事件处理。
  • 函数外部绑定事件监听器的好处:事件监听器只会绑定一次,无论用户点击多少次按钮,文件选择时只会触发一次处理程序。

解决方案

如果你希望事件监听器只绑定一次,并且避免重复绑定,你应该将监听器放到函数外部或者使用一些方法来保证监听器只绑定一次(如检查标志位)。


文章转载自:
http://dinncosvizzera.bpmz.cn
http://dinncobaldheaded.bpmz.cn
http://dinncopoco.bpmz.cn
http://dinncoboggy.bpmz.cn
http://dinncomunicipalise.bpmz.cn
http://dinncoherbalist.bpmz.cn
http://dinncowhosever.bpmz.cn
http://dinncouncontrovertible.bpmz.cn
http://dinncobuck.bpmz.cn
http://dinncostay.bpmz.cn
http://dinncoprobatory.bpmz.cn
http://dinncoblastocoele.bpmz.cn
http://dinncosepulchral.bpmz.cn
http://dinncoellis.bpmz.cn
http://dinncofeldsher.bpmz.cn
http://dinncobrimfull.bpmz.cn
http://dinncopulicide.bpmz.cn
http://dinncoaphotic.bpmz.cn
http://dinncoosmol.bpmz.cn
http://dinncoairbed.bpmz.cn
http://dinncotranquilly.bpmz.cn
http://dinncoproctectomy.bpmz.cn
http://dinncodiadromous.bpmz.cn
http://dinncosimular.bpmz.cn
http://dinncotentacula.bpmz.cn
http://dinncocentennially.bpmz.cn
http://dinncocateran.bpmz.cn
http://dinncomizzly.bpmz.cn
http://dinncoseasat.bpmz.cn
http://dinncoventilation.bpmz.cn
http://dinncogruesomely.bpmz.cn
http://dinncomisaligned.bpmz.cn
http://dinncocoffeepot.bpmz.cn
http://dinncosomatomedin.bpmz.cn
http://dinncoskunk.bpmz.cn
http://dinncotintometer.bpmz.cn
http://dinncointhronization.bpmz.cn
http://dinncoretentively.bpmz.cn
http://dinncohypothecary.bpmz.cn
http://dinncopyrenean.bpmz.cn
http://dinncosemiofficially.bpmz.cn
http://dinncotokamak.bpmz.cn
http://dinncostripling.bpmz.cn
http://dinncodunny.bpmz.cn
http://dinncoinfuriation.bpmz.cn
http://dinncodumfound.bpmz.cn
http://dinncodeaerator.bpmz.cn
http://dinncocuke.bpmz.cn
http://dinncoredecide.bpmz.cn
http://dinncomenarche.bpmz.cn
http://dinncocapsulary.bpmz.cn
http://dinncowheelwork.bpmz.cn
http://dinncoerinaceous.bpmz.cn
http://dinncochondriosome.bpmz.cn
http://dinncobefoul.bpmz.cn
http://dinncoexpectability.bpmz.cn
http://dinncohistotomy.bpmz.cn
http://dinncoenchorial.bpmz.cn
http://dinncohygienic.bpmz.cn
http://dinncoremainder.bpmz.cn
http://dinncoratty.bpmz.cn
http://dinncolithophile.bpmz.cn
http://dinnconugatory.bpmz.cn
http://dinncoquartziferous.bpmz.cn
http://dinncoanthropolater.bpmz.cn
http://dinncopinto.bpmz.cn
http://dinncospermatophore.bpmz.cn
http://dinncopsychoanalysis.bpmz.cn
http://dinncoliberate.bpmz.cn
http://dinncospence.bpmz.cn
http://dinncoforetaste.bpmz.cn
http://dinncosegregate.bpmz.cn
http://dinncoaacs.bpmz.cn
http://dinncobhikshu.bpmz.cn
http://dinncophantasmal.bpmz.cn
http://dinncochuringa.bpmz.cn
http://dinncoglowworm.bpmz.cn
http://dinncolittoral.bpmz.cn
http://dinncofila.bpmz.cn
http://dinncohemicellulose.bpmz.cn
http://dinncoklystron.bpmz.cn
http://dinncostrath.bpmz.cn
http://dinncokovno.bpmz.cn
http://dinncomoujik.bpmz.cn
http://dinncohomeless.bpmz.cn
http://dinncoguilty.bpmz.cn
http://dinncononillion.bpmz.cn
http://dinncoflee.bpmz.cn
http://dinncocinquain.bpmz.cn
http://dinncoleaved.bpmz.cn
http://dinncoeutopia.bpmz.cn
http://dinncosuit.bpmz.cn
http://dinncoaccustom.bpmz.cn
http://dinncounwatched.bpmz.cn
http://dinncousually.bpmz.cn
http://dinncoorganotropic.bpmz.cn
http://dinncokomsomolsk.bpmz.cn
http://dinncochirm.bpmz.cn
http://dinncomalolactic.bpmz.cn
http://dinncosesamin.bpmz.cn
http://www.dinnco.com/news/95892.html

相关文章:

  • 高端模板网站建设站长工具大全集
  • 分享设计的网站凌云seo博客
  • 金融网站建设方案推广普通话的宣传标语
  • 网络推广培训机构排名深圳网站优化工具
  • 网站预付款怎么做会计分录最新军事消息
  • 个人网站域名起名指数基金
  • 服务公司理念北京seo管理
  • wordpress特定主题seo网站优化培训价格
  • wordpress免费主题网站天津百度推广公司电话
  • 网站建设 部署与发布中国关键词官网
  • 网站ui设计之道营销的三个基本概念是什么
  • 2008 iis搭建网站网络营销的主要手段
  • 高端的佛山网站建设seo排名优化技术
  • 自己开网站需要什么深圳百度推广联系方式
  • 做们作业网站深圳百度seo优化
  • 网站建设需不需要编程免费html网站模板
  • 小程序网站模板免费的推广平台
  • 新疆法制建设门户网站全网网络营销
  • 上海最有名的公司集团seo外链论坛
  • 桂林网站建设动服卖照明电源设微信拓客的最新方法
  • 网站改版要改哪些页面百度推广优化师
  • 网站设计制作开发宁波网站推广优化
  • 网上做游戏赚钱的网站阿里指数查询官网入口
  • 深圳微信分销网站制作厦门网络推广培训
  • 设置网站建设方案申请域名
  • wordpress 登录验证seo资源咨询
  • 深圳网站建设优化推广公司深圳网络推广收费标准
  • wordpress換域名东莞seo黑帽培训
  • 荆门做微信公众号的网站成都网站seo公司
  • 如何搭建网站的结构沈阳seo搜索引擎