使用js将字符串附加到图像src [英] append string to image src using js

查看:165
本文介绍了使用js将字符串附加到图像src的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个页面,其中有< img src = / images / product / whatever-image.jpg alt = />

I've got a page where there is <img src="/images/product/whatever-image.jpg" alt="" />

我想要它,以便在加载页面时,在字符串src的值后附加字符串?Action = thumbnail,从而使其成为 src = /images/product/whatever-image.jpg?Action=thumbnail

I want it so that upon loading of the page, the string "?Action=thumbnail" is appended to src's value, making it src="/images/product/whatever-image.jpg?Action=thumbnail"

如何使用js实现此目标?

how do I achieve this using js?

推荐答案

window.addEventListener('load', function(){
    /* assuming only one img element */
    var image = document.getElementsByTagName('img')[0];

    image.src += '?Action=thumbnail';
}, false);

注意,更改图像源将从服务器重新获取图像,甚至如果图像相同。

Note, changing the source of the image will "re-fetch" the image from the server — even if the image is the same. This will be better done on the server-side.

评论后更新: >

Update after comment:

window.addEventListener('load', function(){
    /* assuming only one div with class "divclassname" and img is first child */
    var image = document.getElementsByClassName('divclassname')[0].firstChild;

    image.src += '?Action=thumbnail';
}, false);    

这篇关于使用js将字符串附加到图像src的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆