问题
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