我想知道是否有机会只选择父 div 而不是其子 div。一个简单的例子是这样的:
<!DOCTYPE html>
<html>
<body onclick="myFunction(event)">
<div style="border: solid black 2px">
<p>Click on a paragraph. An alert box will alert the element
that triggered the event.</p>
<p><strong>Note:</strong> The target property returns the element that
triggered the event, and not necessarily the eventlistener's element.</p>
</div>
<script>
function myFunction(event) {
alert(event.target.nodeName);
}
</script>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)
我想要的很简单,我想单击 div(不介意它是否在 p 标签上)并接收 div 已被单击的消息。现在,如果我单击 p 标签,我会收到 p 消息。
事实上,我试图仅选择 div 而不是 p 标签,例如更改其所有颜色。谢谢你们。