Why does appending to innerHTML in a bookmarklet overwrite the entire page?

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-09 03:34:35

问题


I have this little bookmarklet:

javascript:document.getElementsByTagName("div")[0].innerHTML+="Chuck Norris";

Now it's obviously supposed to take the very first div on the page, and add Chuck Norris into it.

Instead, when pasted on the address bar, Chuck Norris overwrites the page.

Why is this so?

Note: this doesn't occur on Safari...


回答1:


You are not cancelling the action. add void 0; to then end.

javascript:document.getElementsByTagName("div")[0].innerHTML+="Chuck Norris";void 0;



回答2:


I'm not sure what the exact problem is, but I got it working by making a textNode and appending it:

javascript:var d=document.getElementsByTagName("div")[0];var n=document.createTextNode("Chuck Norris");d.appendChild(n);


来源:https://stackoverflow.com/questions/15284627/why-does-appending-to-innerhtml-in-a-bookmarklet-overwrite-the-entire-page

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!