The problem is, is there any differences between e.target.e.target.setAttribute(‘class’, ‘light’) and btn.target.e.target.setAttribute(‘class’, ‘light’) ?
(the code is in “btn.onclick”, which i’ve already write it with an anonymous function – btn.onclick = function (e){…}
)
i tried both of the expression above, and both of them work well.
can i simplely think, “e.target” is equal to “btn”,?
and even in any code like
“elementName.eventname = function(e) {…}”
can i think
“e.target” is equal to the variable “elemntName”?
according to your code
btn = document.querySelector(‘button’);
so it will return the button object
please be careful in case if there many button on your page which will make btn to be array of button objects
thanks alot for your reply, and i think i understand your analysis.
but in fact, the 2nd code block in my problem is, i use “e.target”, and it is also worked.
btn.onclick = function (e) {
if (e.target.getAttribute(“class”) === “dark”) { e.target.setAttribute(‘class’, ‘light’); e.target.textContent = “变亮”;
overlay.style.backgroundColor = “rgba(0,0,0,0.5)”;
}
else { e.target.setAttribute(‘class’, ‘dark’); e.target.textContent = ‘变暗’;
overlay.style.backgroundColor = ‘rgba(0,0,0,0)’;
}
}
and now, i’m reading about the document of Event and Event.target on MDN, i think the answer is there.
and, i just test my code again.
if i change “btn” to “e”, it doesn’t work.
for example, if i change “btn.textContent = “变亮”;” to “e.textContent = “变亮”;”, it doesn’t work.