2016-11-08 99 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()。即我的大寫是錯誤的。

相關問題