通过 JavaScript 更改图像?
关于 Javascript 的快速问题。我正在开发一个 Safari 扩展程序来削减 Google 搜索页面,并且我想将 Google 徽标更改为自定义图像。我的计划是在扩展中添加一个注入的 .js 脚本。
到目前为止,我已经尝试过这个:
document.getElementById('img#hplogo').innerHTML =
"<img alt="Google" height="95" id="hplogo" src="logo3w.png" width="275"
style="padding-top:136px" onload="window.lol&&lol()">"
为了澄清一些,徽标图像位于 Google 主页上的 ID 下,为“hplogo”,或者根据 Safari Web Inspector,为“img#hplogo”。显然,我想用我自己的 logo3w.png 替换 src,该 logo3w.png 将位于扩展文件夹的根目录中(因此,据我所知,不需要高级目录)。
如果我能在命令方面指出正确的方向,那将非常有帮助,但实际上任何和所有的帮助都是值得赞赏的。谢谢!
Quick question regarding Javascript. I'm working on a Safari Extension for paring down the Google Search page, and I'd like to change the Google logo to a custom image. My plan is to have an injected .js script to put in the extension.
So far, I've tried this:
document.getElementById('img#hplogo').innerHTML =
"<img alt="Google" height="95" id="hplogo" src="logo3w.png" width="275"
style="padding-top:136px" onload="window.lol&&lol()">"
For some clarification, the logo image is under the ID on the Google homepage as "hplogo" or according to Safari Web Inspector, "img#hplogo". I want to replace the src, obviously, with my own logo3w.png that will be located in the root of the extension folder (thus, AFAIK, no advanced directory is needed).
If I could be pointed in the right direction command-wise, that'd be really helpful, but really any and all help is appreciated. Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您想要执行的操作:
请注意,“img#hplogo”并不是说
img
元素的 id 是“img#hplogo”,而是说您正在查看“img”元素其 ID 为“hplogo”。因此,当使用document.getElementById()
时,您只需要传递“hplogo”即可。在 CSS 中,您可能会说:类似地,对于像 jQuery 这样支持 CSS 样式元素选择器的东西,您可能会说:
但是对于
document.getElementById()
,您需要传递的所有内容(以及您可以传递的所有内容)是“hp标志”。You want to do:
Note that the "img#hplogo" is not saying that the id of the
img
element is "img#hplogo", it is saying that you are looking at an "img" element whose id is "hplogo". So when usingdocument.getElementById()
you only need to pass "hplogo". In CSS you might say:And similarly with something like jQuery that supports CSS-style element selectors you might say:
But for
document.getElementById()
all you need to pass (and all you can pass) is "hplogo".您可能想要更改图像的 src 属性:
You may want to change the
src
attribute of the image:更改“innerHTML”属性会更改内部 HTML,而不是元素本身。您要做的就是找到该元素并更改它的“src”属性。
Changing the "innerHTML" property changes the inner HTML, not the element itself. The thing you want to do is find the element and change it's "src" property.