替换给定的 来源
我用 Google Weather API 制作了一个插件,目前正在从 Google 的 API 中提取图像。对于阳光明媚的日子,我正在拉 http://www.google.com//ig/images/weather/sunny.gif
。
我正在寻找一种方法,以便在拉出 sunny.gif 时,用我选择的图像替换。
这可以通过 jQuery 或其他方式实现吗?
多谢
I made a plugin with Google Weather API and I am currently pulling the images from Google's API. For a sunny day I'm pulling http://www.google.com//ig/images/weather/sunny.gif
.
I am looking for a way so that when ever sunny.gif is pulled, replace with with an image of my choice.
Is that possible with jQuery or other way?
Thanks alot
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
取决于您接收/传递图像的方式,只需将其设置为变量并检查它即可。
这是这个简单问题的简单/惰性解决方案,如果您的问题更复杂,您可以将其变成一个检查和替换晴天、暴风雨等的函数...
PHP/JS 反模式:
但老实说,如果您是在 PHP 中接收图像,然后我只需在服务器端操作它并将其显示给客户端,而不是让 JavaScript 来做...
Depends on how you're receiving/passing the image, just set it to a variable and check for it.
This is the easy/lazy solution to this simple problem, if your problem is more complex you could turn this into a function that checks and replaces sunny, stormy, etc...
PHP/JS anti-pattern:
But honestly if you're receiving the image in PHP, then I'd just manipulate it server-side and display it to the client then rather than have JavaScript do it...
有一个实时 document.images文档中所有 img 元素的集合。您可以根据您喜欢的逻辑有条件地更改任何特定图像的 src 属性:
您可能还想访问图像的 parentNode 及其一些属性。
但在我看来,这在服务器上做得更好。
There is a live document.images collection of all img elements in the document. You can conditionally change the src property of any particular image based on whatever logic you like:
You may want to access the image's parentNode and some of its properties too.
But is seems to me that this is much better done on the server.