Javascript onclick增加img网址 [英] Javascript onclick increase img url

查看:132
本文介绍了Javascript onclick增加img网址的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在制作一个小程序,用于显示网站上的图像.如何使用onclick事件,使得每次点击都会增加url的数量,例如/images/P100_01.gif-> /images/P101_01.gif并以HTML格式打印图像?

I''m making a small program that displays the images from a website. How it would be possible to use the onclick event in such a way that each click will increase the number of url eg / images/P100_01.gif -> / images/P101_01.gif and print the image in HTML?

推荐答案

从以下示例中学习和改编:
Learn and adapt from the following example:
<!DOCTYPE html>
<html>
<body>
<p>Click to get next image</p>
<image src="/images/P100_01.gif" id="img">
<button type="button" onclick="getNextImage()">Get Image</button>
<script>
function getNextImage() {
// get the old image source
var oldImageSource = document.getElementById("img").src;
alert(oldImageSource);
// extract the ''100''
var str = oldImageSource.slice(9,12);
// convert ''100'' to number
var oldNum = Number(str);
alert(oldNum);
// increase 100 by 1
var newNum = oldNum + 1;
alert(newNum);
// replace the old number in the old image source by the new one
var newImageSource = oldImageSource.replace(oldNum, newNum); 
alert(newImageSource);
}
</script>
</body>
</html> 


JavaScript字符串对象 [


Read more on JavaScript String Object[^]


这篇关于Javascript onclick增加img网址的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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