gpt4 book ai didi

javascript - 如何获取点击元素的样式 - "currentTarget"

转载 作者:行者123 更新时间:2023-12-02 16:08:11 24 4
gpt4 key购买 nike

<分区>

currentTarget 在我添加 style html 元素时起作用。如何在不向 html 元素添加 style 的情况下访问被单击元素的 style 文件?

const all = document.querySelectorAll(".all");

all.forEach((button) => {
button.addEventListener("click", (e) => {
let bgColor = e.currentTarget.style.backgroundColor;
console.log(bgColor);
});
});
div {
color: white;
height: 2em;
width: 15em;
margin: 2em;
padding: 2em;

background-color: royalblue;
}

span {
color: white;
height: 2em;
width: 15em;
margin: 2em;
padding: 2em;

background-color: red;
}
<div class="all">I'm a thin, big and insulated also soft, prickly box.</div>

<span style="background-color: black;" class="all">I am useless</span>

https://jsfiddle.net/bcz0t6gx/⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣ ⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com