首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> 更多程式語言 >> 程式設計疑難解答

jQuery this.children() 返回 DOM 對象無 jQuery 方法

     編輯:程式設計疑難解答

錯誤代碼

$('.content').hover(function() {
    var myhtml = $(this).children();
    myhtml[1].hide(); // 錯誤:[1] 取得原生 DOM 元素,無 hide()
});

原因

myhtml[1] 返回原生 DOM 元素,不具備 jQuery 方法(hide, css, 等)。

正確寫法

myhtml.eq(1).hide();        // jQuery 方法,返回 jQuery 對象
// 或
$(myhtml[1]).hide();        // 重新包裝為 jQuery 對象

鑒別

  • [] 索引 → 原生 DOM
  • .eq() / .get() → jQuery 對象 / 原生 DOM
  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved