对JS中addEventListener底层实现原理的个人理解

JavaScript事件监听:从addEventListener到事件委托与性能优化 事件监听是前端开发中实现用户交互的核心机制,它允许JavaScript代码响应用户操作或浏览器状态变化。其基本原理基于DOM事件流,包括捕获、目标和冒泡三个阶段,这为事件委托等高级模式奠定了基础。理解事件流和事件对象(Event)是掌握事件系统的关键,它能显著提升代码性能和可维护性。在工程实践中,事件委托通过利用事件冒泡,将监听器绑定在父元素而非每个子元素上,从而减少内存占用、简化动态内容处理,是优化大规模列表或表格交互的常用技术。此外,针对scroll、resize等高频率事件,结合防抖(Debounce 阅读详情

今天,我学到了js中的

addEventListener

然后我决定,自己制作一个简单的事件监听器!

当我们需要用一个按钮的单击去触发两个方法的时候,会发现前一个总会被第二个方法的结果覆盖掉,我现在定义了一个id为“btn”的按钮:

btn = document.getElementById("btn");
p1 = document.getElementsByTagName("p")[0];
p2 = document.getElementsByTagName("p")[1];

btn.onclick = function fn1() {
    p1.innerText = "我是第一个方法";
};
btn.onclick = function fn2() {
    p2.innerText = "我是第二个方法";
};

这时候我们去单击按钮,会显示
在这里插入图片描述
很明显只显示了第二个方法的结果,这时候我们就需要使用事件监听器,

btn.addEventListener("click",fn1);
btn.addEventListener("click",fn2);
function fn1() {
    p1.innerText = "我是第一个方法";
}
function fn2() {
    p2.innerText = "我是第二个方法";
}

再次单击按钮查看结果:
在这里插入图片描述
第一个方法的结果并不会被覆盖掉,那到底是什么原理让它没有覆盖第

Js on及addEventListener原理用法区别解析 主要介绍了Js on及addEventListener原理用法区别解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下 立即下载

相关推荐

Windows 11 Docker Desktop 数据迁移:WSL 2 发行版导出/导入 5步法详解

本文详细介绍了Windows 11下Docker Desktop数据迁移的5步法,通过WSL 2发行版的导出/导入技术,实现C盘空间释放和存储空间优化。该方法支持无损迁移容器、镜像和卷数据,并提供异常处理和进阶优化技巧,帮助开发者彻底解决Docker Desktop存储空间不足的问题。

weixin_30908103的博客 496

Simulink实战:5步搞定固定翼飞行器六自由度动力学建模(附模型下载)

本文提供了一份详细的Simulink实战指南,通过5个核心步骤手把手教你构建固定翼飞行器的六自由度动力学模型。内容涵盖从顶层架构设计、核心方程手动实现,到模型集成、测试调试及进阶优化的完整流程,旨在帮助航空航天学生和工程师将抽象理论转化为可运行的仿真模型,并附有模型下载。

perl8的博客 641

【学习笔记】一文搞懂IEPE传感器

本文介绍了IEPE(集成电路压电式)传感器的工作原理及其在振动监测中的应用。通过压电效应原理,IEPE传感器将机械振动转换为电信号,内置电荷放大器将高阻抗电荷信号转换为低阻抗电压信号输出。还介绍了传感器的三轴测量结构、4mA恒流源供电机制以及灵敏度与量程的关系,并通过风扇实验演示了故障诊断过程。实验使用USB-3123采集卡和SRD-1104信号调理器,展示了正常与异常状态下的振动数据差异,验证了IEPE传感器在设备状态监测中的有效性。

m0_62969865的博客 2830

LSM6DSOW陀螺仪轮询驱动:从I2C/SPI配置到数据读取全解析

惯性测量单元(IMU)是现代嵌入式系统中实现姿态感知与运动跟踪的核心传感器,其通过加速度计和陀螺仪测量线性加速度与角速度。在嵌入式开发中,轮询(Polling)作为一种基础且可靠的通信方式,通过主循环主动查询传感器状态,简化了初始调试与验证流程。对于I2C/SPI接口的数字传感器,掌握寄存器配置、数据就绪标志检查及原始数据转换是通用的驱动方法,能够快速验证传感器工作状态并理解数据流。LSM6DSOW作为一款高性能6轴IMU,支持可配置的量程与输出数据速率,适用于无人机飞控、手持云台等高动态场景。本文以LSM

weixin_28728265的博客 563

基于PCL的电力线点云提取与分割实现

本文基于PCL库实现电力线点云提取与分割,包含6个核心处理阶段:文件读取与结果文件夹创建、高程滤波分割候选点云、欧式聚类去噪、电力线分段提取、球形邻域搜索补全点云、最终分割与可视化保存。通过PCA计算点云线性特征(阈值0.91)筛选电力线点,采用欧式聚类(容忍度3.0)去除噪点,并针对不同规模聚类差异化设置球形搜索半径(4.0/6.0)补全点云。最终通过欧式聚类分割并可视化带颜色点云。算法对高程滤波阈值、聚类参数等具有敏感性,需根据点云特性调整优化。

weixin_45196569的博客 199

半导体设备系列(2) 半导体设备与工厂控制仿真器Demo编写

基于C/S架构用C++编写半导体设备仿真器与工厂控制仿真器

追赶时代的博客 611

运营商拼命封杀的PCDN到底是个啥?

最近一段时间,很多宽带用户发现自家的网速莫名其妙变慢了,甚至收到了运营商发来的“违规使用宽带”警告短信,严重的甚至直接被断网。很多人一头雾水:我交了钱办的宽带,自己买的插件或者京东云、网心云这些硬件赚点“电费”,怎么就成了运营商眼中的“过街老鼠”?今天,我们就把这层窗户纸捅破,深度剖析这个让互联网大厂省下几十亿、让运营商恨得牙痒痒、让用户夹在中间进退两难的技术黑话——。

网络技术联盟站 942

第十二章 供水管网水力模型软件介绍

本文介绍了国内外主要供水管网建模软件及其特点,重点分析了某高建模软件V14的创新功能。国际主流软件包括开源的EPANET、商业软件InfoWorksWS、WaterGEMS和MIKEURBAN,各具特色但存在应用局限。某高V14软件实现了管网GIS与模型一体化管理,采用一G多网、一网多模、一模多案、网模合一技术路线,支持全信息模型建立和动态数据更新。其创新点包括水量分配理念革新(精确/模糊匹配、三种用水模式)和首创Web服务功能。通过并发计算、GPU加速等技术优化性能,具有较高性价比。

weixin_45258875的博客 215

深度学习工具包 DeepLearningToolbox

深度学习工具包Deprecation notice.-----This toolbox is outdated and no longer maintained.There are much better tools available for deep learning than this toolbox, e.g. [Theano](http://deeplearning.net/software/theano/), [torch](http://torch.ch/) or [tensorflow](http://www.tensorflow.org/)I would suggest you use one of the tools mentioned above rather than use this toolbox.Best, Rasmus.DeepLearnToolbox================A Matlab toolbox for Deep Learning.Deep Learning is a new subfield of machine learning that focuses on learning deep hierarchical models of data.It is inspired by the human brain's apparent deep (layered, hierarchical) architecture.A good overview of the theory of Deep Learning theory is[Learning Deep Architectures for AI](http://www.iro.umontreal.ca/~bengioy/papers/ftml_book.pdf)For a more informal introduction, see the following videos by Geoffrey Hinton and Andrew Ng.* [The Next Generation of Neural Networks](http://www.youtube.com/watch?v=AyzOUbkUf3M) (Hinton, 2007)* [Recent Developments in Deep Learning](http://www.youtube.com/watch?v=VdIURAu1-aU) (Hinton, 2010)* [Unsupervised Feature Learning and Deep Learning](http://www.youtube.com/watch?v=ZmNOAtZIgIk) (Ng, 2011)If you use this toolbox in your research please cite [Prediction as a candidate for learning deep hierarchical models of data](http://www2.imm.dtu.dk/pubdb/views/publication_details.php?id=6284)```@MASTERSTHESIS\{IMM2012-06284, author = "R. B. Palm", title = "Prediction as a candidate for learning deep hierarchical models of data", year = "2012",}```Contact: rasmusbergpalm at gmail dot comDirectories included in the toolbox-----------------------------------`NN/` - A library for Feedforward Backpropagation Neural Networks`CNN/` - A library for Convolutional Neural Networks`DBN/` - A library for Deep Belief Networks`SAE/` - A library for Stacked Auto-Encoders`CAE/` - A library for Convolutional Auto-Encoders`util/` - Utility functions used by the libraries`data/` - Data used by the examples`tests/` - unit tests to verify toolbox is workingFor references on each library check REFS.mdSetup-----1. Download.2. addpath(genpath('DeepLearnToolbox'));Example: Deep Belief Network---------------------```matlabfunction test_example_DBNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit RBM and visualize its weightsrand('state',0)dbn.sizes = [100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);figure; visualize(dbn.rbm{1}.W'); % Visualize the RBM weights%% ex2 train a 100-100 hidden unit DBN and use its weights to initialize a NNrand('state',0)%train dbndbn.sizes = [100 100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);%unfold dbn to nnnn = dbnunfoldtonn(dbn, 10);nn.activation_function = 'sigm';%train nnopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.10, 'Too big error');```Example: Stacked Auto-Encoders---------------------```matlabfunction test_example_SAEload mnist_uint8;train_x = double(train_x)/255;test_x = double(test_x)/255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit SDAE and use it to initialize a FFNN% Setup and train a stacked denoising autoencoder (SDAE)rand('state',0)sae = saesetup([784 100]);sae.ae{1}.activation_function = 'sigm';sae.ae{1}.learningRate = 1;sae.ae{1}.inputZeroMaskedFraction = 0.5;opts.numepochs = 1;opts.batchsize = 100;sae = saetrain(sae, train_x, opts);visualize(sae.ae{1}.W{1}(:,2:end)')% Use the SDAE to initialize a FFNNnn = nnsetup([784 100 10]);nn.activation_function = 'sigm';nn.learningRate = 1;nn.W{1} = sae.ae{1}.W{1};% Train the FFNNopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.16, 'Too big error');```Example: Convolutional Neural Nets---------------------```matlabfunction test_example_CNNload mnist_uint8;train_x = double(reshape(train_x',28,28,60000))/255;test_x = double(reshape(test_x',28,28,10000))/255;train_y = double(train_y');test_y = double(test_y');%% ex1 Train a 6c-2s-12c-2s Convolutional neural network %will run 1 epoch in about 200 second and get around 11% error. %With 100 epochs you'll get around 1.2% errorrand('state',0)cnn.layers = { struct('type', 'i') %input layer struct('type', 'c', 'outputmaps', 6, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %sub sampling layer struct('type', 'c', 'outputmaps', 12, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %subsampling layer};cnn = cnnsetup(cnn, train_x, train_y);opts.alpha = 1;opts.batchsize = 50;opts.numepochs = 1;cnn = cnntrain(cnn, train_x, train_y, opts);[er, bad] = cnntest(cnn, test_x, test_y);%plot mean squared errorfigure; plot(cnn.rL);assert(er<0.12, 'Too big error');```Example: Neural Networks---------------------```matlabfunction test_example_NNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);% normalize[train_x, mu, sigma] = zscore(train_x);test_x = normalize(test_x, mu, sigma);%% ex1 vanilla neural netrand('state',0)nn = nnsetup([784 100 10]);opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samples[nn, L] = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.08, 'Too big error');%% ex2 neural net with L2 weight decayrand('state',0)nn = nnsetup([784 100 10]);nn.weightPenaltyL2 = 1e-4; % L2 weight decayopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex3 neural net with dropoutrand('state',0)nn = nnsetup([784 100 10]);nn.dropoutFraction = 0.5; % Dropout fraction opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex4 neural net with sigmoid activation functionrand('state',0)nn = nnsetup([784 100 10]);nn.activation_function = 'sigm'; % Sigmoid activation functionnn.learningRate = 1; % Sigm require a lower learning rateopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex5 plotting functionalityrand('state',0)nn = nnsetup([784 20 10]);opts.numepochs = 5; % Number of full sweeps through datann.output = 'softmax'; % use softmax outputopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex6 neural net with sigmoid activation and plotting of validation and training error% split training data into training and validation datavx = train_x(1:10000,:);tx = train_x(10001:end,:);vy = train_y(1:10000,:);ty = train_y(10001:end,:);rand('state',0)nn = nnsetup([784 20 10]); nn.output = 'softmax'; % use softmax outputopts.numepochs = 5; % Number of full sweeps through dataopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, tx, ty, opts, vx, vy); % nntrain takes validation set as last two arguments (optionally)[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');```[![Bitdeli Badge](https://d2weczhvl823v0.cloudfront.net/rasmusbergpalm/deeplearntoolbox/trend.png)](https://bitdeli.com/free "Bitdeli Badge")

文档:亲测可用解决vnc灰屏,黑屏,鼠标大黑叉情况

主要解决vnc-viewer灰屏,黑屏,鼠标大黑叉等现象,详细见文档。

pscad IEEE标准系统搭建说明

pscad IEEE标准系统搭建说明,包含典型的IEEE 9节点、14节点、30节点、39节点和118节点系统。内含标准测试系统的原始数据和潮流结果。

精雕软件(JDpaint) 5.5 绿色版

精雕软件(JDpaint) 5.5 绿色版精雕软件(JDpaint) 5.5 绿色版精雕软件(JDpaint) 5.5 绿色版

上一篇: JS中encodeURI和encodeURIComponent两种编码方式的区别
下一篇: JS中div.style.left和div.offsetLeft的区别
HarryMing
博客等级 码龄7年 11粉丝 24原创
评论 2
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值