Using x.innerHTML += y
is equivalent to x.innerHTML = x.innerHTML + y;
This means that you are completely overwriting the old document with a new document - it may appear visually the same, but under the hood you've just nuked every single reference to everything.
If a bit of JavaScript elsewhere in the page used something like var container = document.getElementById('container');
, in order to save a reference, well that reference is now gone.
If there are any event listeners bound to elements in the document, those are gone too because the elements were nuked and replaced with identical-looking ones.
If you want to add your context menu to the page, you should do something like:
var menu = document.createElement('div');
menu.id = 'menu';
menu.innerHTML = "Your menu HTML here";
document.body.appendChild(menu);
This will add the new element to the page without nuking the whole thing.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…