javascript - 如何使用javascript查找点击元素的类索引?
问题描述
我有一个基于选项卡的网站,其中通过克隆默认选项卡视图动态创建选项卡,但是,在不知道每个元素的索引号的情况下,无法在每个选项卡上执行逻辑。
function search(){
window.addEventListener("keyup", function(event) {
if (event.keyCode === 13) {
event.preventDefault();
alert(click)
var input = document.getElementsByClassName("myTextInputID")[click].value;
if (input.includes("https://www.amazon.co.uk/")) {
document.getElementsByClassName("prodcont")[click].style.display = "block"
document.getElementsByClassName("imgcont")[click].innerHTML = '<img class="image" src="https://www.needforseat.de/media/image/22/15/b3/MAXNOMIC-R-_LVL_Edition_Pro_YkaFLtIVOgbL1_320x463.jpg">';
document.getElementsByClassName("h1")[click].innerHTML = prodName;
document.getElementsByClassName("containFORM")[click].style.backgroundColor = "white"
} else {
URLerror.appendChild(urlerror);
var cont = document.getElementsByClassName("containFORM")[click];
cont.insertAdjacentElement("afterend", URLerror);
setTimeout(function() {
URLerror.remove();
}, 2000);
}
}
});
}
我设法想出的唯一解决方案是遍历所有类并给它们一个name
与它们的类号匹配的值,然后每当单击选项卡按钮时,它都会返回其名称。但是,这仅适用于前 2 个选项卡,并且仅上升到一个索引号,但是当我为其值添加警报时,clicked
它显示了正确的值,但代码在类索引 [1] 上运行,这非常令人困惑。
<div class="tabcontainer" id="tabcon" style="background-color: aquamarine">
<ul id="navigation1">
<li class="tabli" id="button1"><button class="tabs" id="default" onclick="openPage('0', this, 'red'); buttonOnclick(this.name);"></button></li>
<ul id="navigation2">
<li id="tabli">
<button onclick="newTab()" class="addbut" style="text-align: center">+</button>
</li>
</ul>
</ul>
</div>
function buttonOnclick(clicked) {
for (let x = 0; x < document.getElementsByClassName("tabs").length; x++){
document.getElementsByClassName("tabs")[x].name=x
click = clicked
}
}
解决方案
如果不知道每个元素的索引号,就无法在每个选项卡上执行逻辑
你会很高兴知道这不是真的。您的事件处理程序是使用一个KeyboardEvent
对象调用的,该对象与所有Event
对象一样,具有target
一个引用事件所针对的元素的属性。从那里您可以使用 CSS 选择器找到它的容器,并且您可以使用(查找 CSS 选择器的第一个匹配项)或(查找所有匹配项)找到closest
该容器中的任何元素。querySelector
querySelectorAll
我无法完全弄清楚您在代码中所做的事情,但这将为您提供.tabcontainer
包含事件目标元素的元素:
const container = event.target.closest(".tabcontainer");
这是一个例子:
window.addEventListener("keyup", function(event) {
// Find the container
const container = event.target.closest(".tabcontainer");
if (!container) {
console.log("Couldn't find container for keyup");
return;
}
// Find the counter element
const counter = container.querySelector(".counter");
// Increment it
counter.textContent = +counter.textContent + 1;
});
<div>Start by clicking here to focus the document, then press <kbd>Tab</kbd> to tab to the "tabs" below and press some keys — watch the counter for that "tab" increase.</div>
<div class="tabcontainer" style="background-color: aquamarine">
<ul>
<li class="tabli" autofocus tabindex="0">this can be tabbed to</li>
<li>Keyups in this tab: <span class="counter">0</span></li>
</ul>
</div>
<div class="tabcontainer" style="background-color: aquamarine">
<ul>
<li class="tabli" autofocus tabindex="0">this can be tabbed to</li>
<li>Keyups in this tab: <span class="counter">0</span></li>
</ul>
</div>
<div class="tabcontainer" style="background-color: aquamarine">
<ul>
<li class="tabli" autofocus tabindex="0">this can be tabbed to</li>
<li>Keyups in this tab: <span class="counter">0</span></li>
</ul>
</div>
<div class="tabcontainer" style="background-color: aquamarine">
<ul>
<li class="tabli" autofocus tabindex="0">this can be tabbed to</li>
<li>Keyups in this tab: <span class="counter">0</span></li>
</ul>
</div>
<div class="tabcontainer" style="background-color: aquamarine">
<ul>
<li class="tabli" autofocus tabindex="0">this can be tabbed to</li>
<li>Keyups in this tab: <span class="counter">0</span></li>
</ul>
</div>
推荐阅读
- c# - 从 TokenValidatedContext 获取签名
- marklogic - 具有编校超时的 MLCP 复制命令
- php - foreignId 字段的变量类型以字符串形式返回,而不是整数 | 拉拉维尔 | 移民
- sql - 查找具有重复字符串数据的行
- asp.net-mvc - 在 MVC 中使用 @Url.Action 进行重定向
- docker - 是否可以在具有高山风味的 docker 容器内构建 AOSP?
- php - 如何在 PHP 中将音频文件转换为 json?
- javascript - 未捕获的 TypeError: $(...).validate 不是 rails 6 webpack 中的函数
- wpf - WPF 异步加载 XAML 的一部分
- ios - Material Design 的 TabsController 显示在安全区域上方 [iOS, Swift, Material Design]