jquery - 点击时 - 找到被点击元素的索引 - 它之前出现了多少次?
问题描述
$(document).on('click', '.productPhoto', function(){
console.log($(this).prevAll(".productPhoto").length); // * this is photo number 3
});
我需要检查点击之前有多少productPhoto
元素(=它的索引)
这不起作用,因为prevAll
是针对兄弟姐妹productPhoto
的,我需要搜索在单击的容器(不是兄弟姐妹)下出现了多少次。
那么-在名为的容器中,单击products
的索引是多少?productPhoto
解决方案
嗯,它很简单 console.log( $( ".productPhoto" ).index( this ) );
推荐阅读
- java - 无法在 JavaFx 中使用 MVC 模式绑定 Listview
- css - 网格项目的叠加
- c++ - 向量的输出
- c++ - 如何在 C++ 中创建包含多种类型对象的列表
- python - 使用 Python eval() 方法执行字符串格式函数并返回值
- swift - SwiftUI:我如何将按钮与 navigationTitle 放在同一行
- node.js - 如何加密 URL 参数 - React.js - Js - Node.js
- python - 一种热编码(映射值问题)
- python - python将mongodb图像与文本合并,使用pymongo和gridfs
- mysql - 为 Django 和 MySQL 配置 CircleCI