javascript - 多个html部分,检查我在哪个部分
问题描述
我有 2 个 HTML 部分,它们都有相同的 id(不在我手中,第三部分 cms)
我目前正在运行一些简单的代码来查看有多少具有此 ID 的 html 部分。
如何设置我单击的获取数组对象?
我的 js 看看有多少部分具有相同的 ID
所以我想要一种可以console.log(arrayItem)
查看我单击的数组对象的方法。
var elms = document.querySelectorAll("[id='sectorpage-strengths']");
for(var i = 0; i < elms.length; i++)
console.log(elms);
console.log(elms[i]);
解决方案
你可以尝试这样的事情并得到this
对象
$('[id="sectorpage-strengths"]').click(function(){
var obj = $(this).text();
console.log("Click Event Triggered By " + obj);
});
section{
border: solid 1px red;
width: 100px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<section id="sectorpage-strengths">Section 1</section>
<section id="sectorpage-strengths">Section 2</section>
推荐阅读
- python - 修改索引由不带 for 循环的列表定义的列表元素
- reactjs - 显示/隐藏 div onClick
- javascript - Vue 3 - Typescript - 对象可能为空问题
- ionic-framework - 离子光总结算法的建议
- apache-spark - 在 Openshift 上使用 spark-submit 以使用特定的 Worker 节点
- python - 如何限制图表上显示的列数
- ada - 对于可能在函数或过程中释放的访问类型,如何处理 **Post** 合约中的“旧”属性?
- java - Hibernate Search + Elasticsearch - 请求中的version_type - 如何实现
- python - 模式验证错误 FastAPI & pydantic
- python - 如何将pytorch的预测转换为普通文本