You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

求助:出现TypeError: Cannot read properties of undefined (reading 'createDocumentFragment')错误

问题解决:TypeError: Cannot read properties of undefined (reading 'createDocumentFragment')

错误原因分析

你的代码存在两个核心问题:

  1. $("document") 写法错误:jQuery中正确的文档就绪写法是$(document),给document加引号会让jQuery去查找名为<document>的HTML标签,该标签不存在,可能引发后续逻辑异常。
  2. 箭头函数导致this指向异常:事件回调使用箭头函数时,this不会指向触发点击的DOM元素,而是继承自外层作用域,此时$(this)是无效的jQuery对象,调用.text()时内部执行DOM操作就会抛出createDocumentFragment相关错误。

修正后的代码

$(document).ready(function() {  
    $(".department-item").on("click", function() {
        console.log($(this).text())
    })
})

替代方案(保留箭头函数)

如果坚持使用箭头函数,可以通过事件对象的target属性获取触发元素:

$(document).ready(() => {  
    $(".department-item").on("click", (e) => {
        console.log($(e.target).text())
    })
})

内容的提问来源于stack exchange,提问作者Juliena

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.23 16:45:59