document.write 在状态栏中
我正在尝试创建自己的书签,但并没有大量的指南可以实现这一点。基本上,现在我试图弄清楚为什么我不能只执行 javascript:document.write("HAI, WORLD"),这会产生替换窗口内容的意外效果。
据我从其他地方了解到,Javascript用方法的返回值替换了窗口的全部内容,所以我也尝试了 javascript:void(document.write("wut")),但整个窗口内容仍然被替换。
I'm trying to create my own bookmarklet, and there aren't exactly a huge number of guides for doing so. Basically, right now I'm trying to figure out why I can't just do javascript:document.write("HAI, WORLD"), which has the unintended effect of replacing the window contents.
It's my understanding from elsewhere that Javascript replaces the entire contents of the window with the return value of a method, so I've also tried javascript:void(document.write("wut")), but the entire window contents are still replaced.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
如果在页面加载完成后调用
document.write()
,则页面的全部内容将被覆盖。这就是该功能的预期效果。如果您希望即时修改 HTML 代码,您有以下几种选择:
document.getElementById("foobar").innerHTML("baz")
document.getElementById("foobar") ).appendChild(document.createTextNode("baz"))
此外,您应该研究像 jQuery 这样的框架,它可以为您处理很多事情:
$("#foobar").html (“巴兹”)
If
document.write()
is called after the page has finished loading, the entire contents of the page will be overwritten. This is the intended effect of the function.If you wish to modify the HTML code on the fly, you have a few options:
document.getElementById("foobar").innerHTML("baz")
document.getElementById("foobar").appendChild(document.createTextNode("baz"))
Additionally, you should look into a framework like jQuery, which can handle a lot of this for you:
$("#foobar").html("baz")
是的,正如 Elijah 所说,或者如果您想更改浏览器的状态栏:
yeah as Elijah said, or if you want to alter the browser's status bar:
这是因为页面已经加载,并且它不知道您要写入的位置。使用
document.documentElement.appendChild(document.createTextNode("HAI, WORLD"))
。That's because the page is already loaded and it doesn't know where you want to write to. Use
document.documentElement.appendChild(document.createTextNode("HAI, WORLD"))
.我试图理解你的“问题”,但我认为你想让它在新窗口中打开,对吗?
然后你需要使用:
javascript:window.open("", "some title");
I try to understand your "question", but I think you'd want to let it open up in a new window, right?
Then you'd need to use:
javascript:window.open("<url to website>", "some title");