通过 JavaScript 更改图像?

发布于 2024-12-02 16:38:40 字数 581 浏览 0 评论 0原文

关于 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&amp;&amp;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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

陈独秀 2024-12-09 16:38:40

您想要执行的操作:

document.getElementById("hplogo").src = "logo3w.png";

请注意,“img#hplogo”并不是说 img 元素的 id 是“img#hplogo”,而是说您正在查看“img”元素其 ID 为“hplogo”。因此,当使用 document.getElementById() 时,您只需要传递“hplogo”即可。在 CSS 中,您可能会说:

img #hplogo {
    display: none;  //or whatever
}
#hplogo {
    display: none;  //or whatever
}

类似地,对于像 jQuery 这样支持 CSS 样式元素选择器的东西,您可能会说:

var image = $("#hpLogo");
var theSameImage = $("img #hplogo");

但是对于 document.getElementById() ,您需要传递的所有内容(以及您可以传递的所有内容)是“hp标志”。

You want to do:

document.getElementById("hplogo").src = "logo3w.png";

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 using document.getElementById() you only need to pass "hplogo". In CSS you might say:

img #hplogo {
    display: none;  //or whatever
}
#hplogo {
    display: none;  //or whatever
}

And similarly with something like jQuery that supports CSS-style element selectors you might say:

var image = $("#hpLogo");
var theSameImage = $("img #hplogo");

But for document.getElementById() all you need to pass (and all you can pass) is "hplogo".

故人爱我别走 2024-12-09 16:38:40

您可能想要更改图像的 src 属性:

document.getElementById('img#hplogo').src = 'path/to/your/image.jpg'

You may want to change the src attribute of the image:

document.getElementById('img#hplogo').src = 'path/to/your/image.jpg'
薔薇婲 2024-12-09 16:38:40

更改“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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文