这段代码中的第二条语句:
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
.
我该如何修复这个错误?
发生这种情况是因为您正在分配给变量$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
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)