Uncaught TypeError: Cannot read property 'click' of null

问题描述

从错误提示来看,表示“未捕获错误类型:无法读取空的属性“click””,可以知道监听click事件的对象没有获取到。

举个栗子:

$("p").click(function () {
    console.log(123)
})

也就是说上面的 $("p") 为 null,获取不到对象。

分析

一般情况下都是 jQuery 的 $ 方法被覆盖。

解决方案

$ 改为 jQuery 使用

jQuery.noConflict(); // 将变量$的控制权让渡给给其他插件或库
jQuery(document).ready(function () {
    var $menu = jQuery(".navbar-nav li a");
    var $menulist = jQuery(".navbar-nav .dropdown .dropdown-menu");
    console.log($menu)
    console.log($menulist)

    $menu.click(function () {
        console.log(123)
    })
})
  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Uncaught TypeError: Cannot read properties of null (reading 'location')是一个错误消息,提示无法读取null的属性(读取'location')。这个错误通常出现在JavaScript代码中,当尝试通过null值访问对象的属性时会出现。 引用中提到了类似的错误消息,但与'location'无关。引用和引用中提到了类似的错误消息,但没有明确指出与'location'有关的具体问题。 根据提供的引用内容,我们无法确定具体的上下文和代码,因此无法提供关于Uncaught TypeError: Cannot read properties of null (reading 'location')的具体解决方案。然而,通常这种错误出现的原因是尝试在一个null或未定义的对象上访问属性。 要解决这个错误,可以按照以下步骤进行排查: 1. 确保代码中没有将变量设置为null或未定义。 2. 检查代码中访问'location'属性的地方,确保它们所针对的对象没有被设置为null。 3. 确保代码中正确初始化了相关对象,以避免null引用错误。 4. 使用调试工具检查代码执行过程中的变量值,以确定错误发生的具体位置和原因。 总的来说,Uncaught TypeError: Cannot read properties of null (reading 'location')错误表明代码中存在尝试在null值上访问属性的问题。通过仔细检查代码并遵循上述步骤,可以找到并解决这个错误。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [Uncaught TypeError: Cannot read properties of null (reading ‘getAttribute‘)](https://blog.csdn.net/weixin_65793170/article/details/129682816)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 33.333333333333336%"] - *2* [报错 Uncaught TypeError: Cannot read properties of null (reading ‘setAttribute‘)](https://blog.csdn.net/qq_50276105/article/details/127173192)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 33.333333333333336%"] - *3* [ASP.NET MVC运行出现Uncaught TypeError: Cannot set property __MVC_FormValidation of null的解决方法](https://download.csdn.net/download/weixin_38731761/13069683)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 33.333333333333336%"] [ .reference_list ]

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值