在新窗口中打开更大版本的图片/ w javascript [英] Opening bigger version of picture in new window /w javascript
问题描述
我已设法使用此代码将图片的较大版本打开到新窗口:
I've managed to get the picture to open the bigger version of the picture into a new window using this code:
<IMG SRC="pic_small.jpg" onClick="view();">
<script language="JavaScript">
function view() {
imgsrc = "pic_big.jpg";
viewwin = window.open(imgsrc,'viewwin', 'width=600,height=300');
}
</script>
我现在遇到的问题是我的网页上有更多图片,我想要不必为每个脚本编写另一个函数,而是为所有图片设置一个函数。换句话说,我希望有一个脚本与上面做同样的事情,但适用于所有这些(如果需要,修改)HTML行
The problem I got now is that I got many more pictures on my webpage and I'd like to not have to write another function for every script but instead having one function for all the pictures. In other words I'd like to have a script that does the same thing as above, but works on all of these (modified if needed) HTML lines
<IMG SRC="pic1_small.jpg" onClick="view();">
<IMG SRC="pic2_small.jpg" onClick="view();">
<IMG SRC="pic3_small.jpg" onClick="view();">
<IMG SRC="pic4_small.jpg" onClick="view();">
我还想知道是否有办法让窗户打开的尺寸与大图。
I was also wondering if there is some way to make the window that opens the the same size as the big picture.
推荐答案
第一个问题
<IMG SRC="pic_small.jpg" onClick="view(this);">
<script language="JavaScript">
function view(img) {
imgsrc = img.src.split("_")[0] + "_big.jpg";
viewwin = window.open(imgsrc,'viewwin', 'width=600,height=300');
}
</script>
更新
关于第二个问题,可以完成。
更新2
这取决于您保留相同格式的图片名称。或者,你可以这样做:
This depends on your image names keeping that same format. Alternatively, you could do this:
<img src="pic_small.jpg" onclick="view('pic_big.jpg')" />
<script type="text/javascript">
function view(imgsrc) {
viewwin = window.open(imgsrc,'viewwin', 'width=600,height=300');
}
</script>
这篇关于在新窗口中打开更大版本的图片/ w javascript的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!