jQuery Error: Syntax error, unrecognized expression: [object HTMLDivElement]
jQuery Error: Syntax error, unrecognized expression: [object HTMLDivElement]
我正在遍历一组元素,然后试图在当时的循环中找到所述元素的某些特定子元素,但出现错误。
我的代码:
var submenu_height;
var secondary_submenus;
var secondary_submenu_height;
// Get the submenus
var submenus = $('#main section.top nav > ul > li > .sub_menu');
// Now loop through each submenu
submenus.each(function(i, element) {
// Get height of submenu
submenu_height = $(this).height();
// Reset our secondary submenu height
secondary_submenu_height = 0;
// Get submenus of this submenu
secondary_submenus = $(this + ' table.sm_wrapper td ul li .sm_wrapper2');
});
问题出在这一行:
secondary_submenus = $(this + ' table.sm_wrapper td ul li .sm_wrapper2');
我尝试使用 element
和 this
,但均无效。我得到的错误是:
Error: Syntax error, unrecognized expression: [object HTMLDivElement] table.sm_wrapper td ul li .sm_wrapper
做我需要的正确方法是什么?
你可能想要
secondary_submenus = $(this).find('table.sm_wrapper td ul li .sm_wrapper2');
您收到错误的原因是您试图将对象 (this
) 添加到字符串并将其用作选择器,因此
...unrecognized expression: [object HTMLDivElement]...
find() 将做的是从 $(this)
的上下文中搜索所传递的选择器
中提到的元素的子项
我正在遍历一组元素,然后试图在当时的循环中找到所述元素的某些特定子元素,但出现错误。
我的代码:
var submenu_height;
var secondary_submenus;
var secondary_submenu_height;
// Get the submenus
var submenus = $('#main section.top nav > ul > li > .sub_menu');
// Now loop through each submenu
submenus.each(function(i, element) {
// Get height of submenu
submenu_height = $(this).height();
// Reset our secondary submenu height
secondary_submenu_height = 0;
// Get submenus of this submenu
secondary_submenus = $(this + ' table.sm_wrapper td ul li .sm_wrapper2');
});
问题出在这一行:
secondary_submenus = $(this + ' table.sm_wrapper td ul li .sm_wrapper2');
我尝试使用 element
和 this
,但均无效。我得到的错误是:
Error: Syntax error, unrecognized expression: [object HTMLDivElement] table.sm_wrapper td ul li .sm_wrapper
做我需要的正确方法是什么?
你可能想要
secondary_submenus = $(this).find('table.sm_wrapper td ul li .sm_wrapper2');
您收到错误的原因是您试图将对象 (this
) 添加到字符串并将其用作选择器,因此
...unrecognized expression: [object HTMLDivElement]...
find() 将做的是从 $(this)
的上下文中搜索所传递的选择器