简体   繁体   中英

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

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...

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

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

我不确定究竟是什么问题,但我通过创建textNode并附加它来实现它:

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

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