简体   繁体   中英

Propagate click event on multiple elements in the same place but not have parent-child relationship

I have a question that how can I propagate a click event on 2 elements in the same place but it doesn't have parent-child relationship. For example, I have this html:

<body>
    <button id="btn1">BUTTON 1</button>  // position fixed with top/left at 10px
    <div>
        <button id="btn2">BUTTON 2</button>  // position fixed with top/left at 10px (same place of button 1)
    </div>
</body>

So in the UI, button 1 and button 2 nearly have the same place, so I want if I click on button 2, the click event is also fired on both button 1 and button 2.

I appreciate any help.

You have to listen to the click event on your button 2 and then call the click event for button 1 as well.

doesn't work if you assigned the same event to the two buttons? or is necessary the other?

function someFuntion() {/* Do something */}
button1.addEventListener('click',somefuntion)
button2.addEventListener('click',somefuntion)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM