Home > front end >  Questions about the event delegation
Questions about the event delegation

Time:09-18

<body>

  • Know no know no, point I should have bounced in hand!

  • Know no know no, point I should have bounced in hand!

  • Know no know no, point I should have bounced in hand!

  • Know no know no, point I should have bounced in hand!

  • Know no know no, point I should have bounced in hand!


<script>
Core principles://events entrusted to the parent node adding listeners, using event bubbling effect each child node
Var ul=document. QuerySelector (" ul ");
Ul. AddEventListener (' click ', function (e) {
//alert (' know no no, I should have bounced in hand! ');
//e. arget this can get us click object
E. arget. Style. BackgroundColor='pink';


})
</script>


Click the li background color will turn into pink, but I think at the time of the next click li click to change the background color of the back to before the original, how to achieve this?

CodePudding user response:

 & lt; script> 
Var ul=document. QuerySelector (" ul ");
Ul. AddEventListener (' click ', function (e) {
E. arget. Style. BackgroundColor=e. arget. Style. The backgroundColor=='pink'? 'white' : 'pink';
})
</script>

CodePudding user response:

If whichever point powder and other white?
<body>

  • Know no know no, point I should have bounced in hand!

  • Know no know no, point I should have bounced in hand!

  • Know no know no, point I should have bounced in hand!

  • Know no know no, point I should have bounced in hand!

  • Know no know no, point I should have bounced in hand!


<script>
Core principles://events entrusted to the parent node adding listeners, using event bubbling effect each child node
Var ul=document. QuerySelector (" ul ");
Var lis=document. QuerySelectorAll (" li ");
Ul. AddEventListener (' click ', function (e) {
Array. The prototype. The forEach. Call (lis, function (value) {
If (value=https://bbs.csdn.net/topics/=e. arget) {
Value. The style. The backgroundColor="pink";
} else {
Value. The style. The backgroundColor="white";
}
});
})
</script>
  • Related