首页 > 解决方案 > 多个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]);

标签: javascripthtmljqueryarrays

解决方案


你可以尝试这样的事情并得到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>


推荐阅读