2016-11-08 174 views
7

第二条语句在此代码:

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 

当我将鼠标悬停在$this.text()在Chrome调试器,我可以看到我想要的价值在title

我该如何解决这个错误?

+2

$这是DOM对象不是,jquery对象,要么包装它像$($ this).text()ot $ this.innerText来阅读文本 –

回答

9

发生这种情况是因为您要为变量$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 
+0

这个作品完美!谢谢! – Cruncher

2

$这是DOM对象不是jQuery对象,无论是包装像$($this).text()$this.innerText读取文本

0

由于这是是第一个结果,对我来说,当我谷歌搜索这个,我会发布我的问题/解决方案。我写了Text()而不是text()。即我的大写是错误的。

相关问题