未捕获的TypeError:$ this.text不是函数
此代码中的第二个语句:
var $this = $(this).children('div.submenu1').children('a.subtile')[0], title = $this.text(), name = $this.attr('node').val;
产生此错误:
Uncaught TypeError: $this.text is not a function
当我将鼠标hover在Chrome调试器中的$this.text()
,我可以在title
看到我想要的值。
我该如何解决这个错误?
发生这种情况是因为您将变量$this
赋予$this
机DOM引用,而不是jQuery引用。 只有后者才能访问jQuery API。
你是通过’到达’类似数组的jQuery堆栈并通过[0]
提取本机引用来实现的。 失去这个,它会起作用:
var $this = $(this).children('div.submenu1').children('a.subtile'), title = $this.text(), //<-- now has access to jQuery API
$这是DOM对象not,jquery对象,要么像$($this).text()
或$this.innerText
来读取文本
因为这是第一个结果,对我来说,当我用Google搜索时,我会发布我的问题/解决方案。 我写过Text()而不是text()。 即我的大写错误。
我遇到了同样的问题,最终我的浏览器禁用了JavaScript。 确保启用JavaScript以在页面上加载。