简体   繁体   中英

How to stop click inside iframe

I display some content inside iframe and this content is html. I want to block click on all hyperlinks. I tried the following and it works. But I do not want to use jQuery. How can I do so with vanilla JS?

jQuery('#htmlFrame').contents().find('body a').on('click', function(event) {
  event.preventDefault();
  event.stopPropagation();
  return false;
});

Haven't tested but it should be equivalent to the below:

const htmlFrame = document.getElementById('htmlFrame');
const links = htmlFrame.querySelectorAll('a');

for (let i = 0; i < links.length; i++) {
  links[i].addEventListener('click', (event) => {
    event.preventDefault();
    event.stopPropagation();
  })
}

你可以创建一个父 div 并应用类似的东西

event.stopPropagation()

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