如何使用 jQuery 选择单个子元素?我查看了 Traversing API 并且知道我可以像这样选择所有直接子 img
元素:
$(this).children('img');
要选择第一个子 img
元素,我可以使用这样的下标:
$(this).children('img')[0];
但我想我有点惊讶我不能这样做:
$(this).child('img'); // no subscript, returns single element
还是我错过了什么?
最佳答案
我认为你想要做的是:
$(this).children('img').eq(0);
这将为您提供一个包含第一个 img 元素的 jquery 对象,而
$(this).children('img')[0];
会给你 img 元素本身。
关于javascript - 如何使用 jQuery 选择单个子元素?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1474089/