未捕获的TypeError:$ this.text不是函数 [英] Uncaught TypeError: $this.text is not a function

查看:110
本文介绍了未捕获的TypeError:$ this.text不是函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

此代码中的第二个陈述:

The second statement in this code:

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

当我悬停时在Chrome调试器中超过 $ this.text()我可以在 title 中看到我想要的值。

When I hover over $this.text() in Chrome debugger I can see the value I want to have in title.

如何修复此错误?

推荐答案

这种情况正在发生,因为你'分配给变量 $ this 本机DOM引用,而不是jQuery引用。只有后者可以访问jQuery API。

This is happening because you're assigning to the variable $this a native DOM reference, not a jQuery reference. Only the latter has access to the jQuery API.

你是通过'到达'类似数组的jQuery堆栈并通过<$提取本机引用来实现的。 C $ C> [0] 。失去这个并且它将起作用:

You're doing this by 'reaching inside' the array-like jQuery stack and extracting the native reference, via [0]. Lose this and it'll work:

var $this = $(this).children('div.submenu1').children('a.subtile'),
title = $this.text(), //<-- now has access to jQuery API

这篇关于未捕获的TypeError:$ this.text不是函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆