为什么80%的码农都做不了架构师?>>>   

css ↓

.wechatBtn {position: relative;}
.wechat {position: absolute; top: 24px; right: -1px; display: none;}

js ↓

function wechatBlock (){
            var wechat = document.getElementsByClassName('wechat');
            wechat.style.display = 'block';
            console.log('2');
}
function wechatNone (){
            var wechat = document.getElementsByClassName('wechat');
            wechat.style.display = 'none';
            console.log('3');
}
window.onload = function(){
            var wechatBtn = document.getElementsByClassName('wechatBtn');
            wechatBtn.addEventListener('mouseover',wechatBlock);
            wechatBtn.addEventListener('mouseout',wechatNone);
            console.log('1');
}

html ↓

<a class="wechatBtn" href="#">
              关注微信
              <div class="QRcode wechat">
                    <span>
                    订阅号
                    </span>
                    <span>
                    商家服务号
                    </span>
              </div>
</a>

事件监听报错

一直报错Cannot set property 'display' of undefined,终于找到原因,getElementsByClassName('');取出的是一个数组,所以要在后面加上[0],或者用querySelector替代getElementsByClassName('');,所以类似的getElementsByTagName('');等等都会出现这个问题。

所以要这样写:

function wechatBlock (){
            var wechat = document.getElementsByClassName('wechat');
            wechat[0].style.display = 'block';
            console.log('2');
}
function wechatNone (){
            var wechat = document.getElementsByClassName('wechat');
            wechat[0].style.display = 'none';
            console.log('3');
}
window.onload = function(){
            var wechatBtn = document.getElementsByClassName('wechatBtn');
            wechatBtn[0].addEventListener('mouseover',wechatBlock);
            wechatBtn[0].addEventListener('mouseout',wechatNone);
            console.log('1');
}

用jQuery写更简单:

window.onload = function(){
            $('.wechatBtn').mouseover(function(){
                $('.wechat').show();
            });
            $('.wechatBtn').mouseout(function(){
                $('.wechat').hide();
            });
}

jQuery很好,但是却有一个小BUG,确切的说这个BUG应该是浏览器事件冒泡所造成的——那就是对于有子元素的父亲同时使用mouseover和mouseout事件并对其进行效果处理(如fadeIn/Out、slideDown/Up...等等)。

window.onload = function(){
            $('.wechatBtn').mouseover(function(){
                $('.wechat').fadeIn("fast");
            }).mouseout(function(){
                $('.wechat').fadeOut("fast");
            });
}

这段代码把鼠标移入.wechat后,就会来回的显示隐藏造成闪烁。不过还是有办法的,可以用hover代替,对执行的动作延迟处理,这样闪烁的问题就解决了,show()和hide()不需要延迟执行,事件冒泡对他们没有影响。

window.onload = function(){
        $(.wechatBtn).hover(

function(){

var $btn =  $('.wechatBtn');

t = setInterval(function(){

$btn.children().slideDown(300);

},300);

},function(){

clearInterval(t);

$(this).children().slideUp();

}

);

}

问题解决了!

转载于:https://my.oschina.net/af666/blog/753796

事件监听一直报错Cannot set property 'display' of undefined相关推荐

  1. 【js监听报错】页面监听js报错问题

    <html> <head> <script type="text/javascript">// 页面监听js报错问题 οnerrοr=handl ...

  2. Oracle数据库监听启动报错

    [root@qyy ~]# su - oracle Last login: Sat May 15 05:46:53 CST 2021 on pts/1 -bash: export: `//ORACLE ...

  3. vue 报错 Cannot read property ‘__ob__‘ of undefined的解决方法

    vue 报错 Cannot read property '__ob__' of undefined的解决方法 参考文章: (1)vue 报错 Cannot read property '__ob__' ...

  4. DateTimePicker 日期时间选择器报错 Cannot read property ‘getHours‘ of undefined, 无法选中`[__ob_: observer__]`时做判断

    我在一次开发中遇到了这个情况:使用DateTimePicker 日期时间选择器,出现无法选中, 报错 Cannot read property 'getHours' of undefined, 这个原 ...

  5. Vue开启Gzip打包异常:webpack打包报错Cannot read property ‘emit‘ of undefined

    Vue开启Gzip打包异常:webpack打包报错Cannot read property 'emit' of undefined 相关代码 const CompressionPlugin = req ...

  6. vue echarts使用map地图 引入china.js报错Cannot read property ‘echarts’ of undefined

    下载china.js导入项目中引入,会报错Cannot read property 'echarts' of undefined import Echarts from "echarts&q ...

  7. uniapp 小程序报错 Cannot read property ‘forceUpdate‘ of undefined

    uniapp 小程序报错 Cannot read property 'forceUpdate' of undefined 1.问题: 解决:配置小程序的ID manifest.json的文件

  8. js报错- cannot set property xxx of undefined

    case1 报错代码 quizList[index] = currentQuiz; quizList[index].learningItem = item; 报错内容 Cannot read prop ...

  9. 升级bigsur_升级 macOS Big Sur 后,程序监听端口报错

    作为一个喜欢尝试新事物,并且有一些安全意识的人,总是会第一时间升级新系统.在 macOS Big Sur 发布稳定版的第一天,我就升级了,从最开始不习惯圆滚滚的图标,现在也习惯了.不过今天在使用过程中 ...

最新文章

  1. tensorflow 回归的例子,包括保存模型和重新预测
  2. wyh 的 Code Style
  3. 【LOJ】#2184. 「SDOI2015」星际战争
  4. 【qduoj】C语言_凯撒密码
  5. 对于局部变量_浅谈Shell函数中全局变量和局部变量
  6. 关于练习美剧听写中碰到的问题
  7. 【招聘内推】推荐策略产品经理-阿里文娱优酷产品直招
  8. 浙大 PAT 甲级1009
  9. 【SQLServer】
  10. 如何用python做俄罗斯方块_你的童年有俄罗斯方块吗?教你用 Python 实现俄罗斯方块!...
  11. ubuntu8.10_深圳源
  12. 并查集详细讲解(转载) 模板
  13. [RMI TCP Connection(10.0.20.175:2181)] DEBUG org.apache.zookeeper.ClientCnxn - Got ping response for
  14. TIMIT语音库下载以及语音库WAV转换相关问题
  15. PowerBi包含什么,以及每一个的介绍
  16. 2019裁员潮,测试被裁了能干什么?
  17. 携程2019校园秋招后台开发笔试题(Java)
  18. sql语句根据身份证号获取年龄
  19. 微信原图暴露的只能是 Exif ,你的隐私不在这!!!
  20. cache是什么?作用是什么?位置在哪?

热门文章

  1. 各种没有由来的问题,干!
  2. [Lua学习]简单链表
  3. [20150611]优化sql遇到问题.txt
  4. linux 下mysql安装配置管理以及优化
  5. DreamWeaver做ASP 第5页
  6. 人际交往的“三有三避”
  7. Eclipse使用时的一些小积累
  8. Nginx的SSL相关指令
  9. ClassPathScanningCandidateComponentProvider 扫描给定包及其子包的类
  10. 在vs code中创建代码片段