在 JavaScript 中的 ClickEvent 上选择图像
Selecting image on ClickEvent in JavaScript
我需要JavaScript,HTML,HTML DOM和DHTML方面的帮助。我对JavaScript不太熟悉,但我正在尝试使用JavaScript和HTML创建网站。情况如下:
div div
main image main image
image1 image1
image2 image2
image3 image3
image4
默认情况下,它将在页面加载时将 image1 显示为主图像,当用户单击该div 下的任何图像时,它应该替换主图像。
function ChangesSrc()
{
if(x == 0)
{
x = 1;
document.getElementById('image1').src = "";
document.getElementById('image1').style.width = "306px";
document.getElementById('image1').style.height = "230px";
}
else if(x == 1)
{
x = 2;
document.getElementById('image1').src = "";
document.getElementById('image1').style.width = "306px";
document.getElementById('image1').style.height = "230px";
}
else if(x == 2)
{
x = 0;
document.getElementById('image1').src = "";
document.getElementById('image1').style.width = "306px";
document.getElementById('image1').style.height = "230px";
}
else
{
x = 0;
document.getElementById('image1').src = "";
document.getElementById('image1').style.width = "306px";
document.getElementById('image1').style.height = "230px";
}
}
</script>
我的问题是我确实必须为点击事件中的每个图像指定图像 src,并且它还按顺序显示图像(如果我先单击最后一个图像;它仍然仅将第一个图像显示为主图像)。
谁能指导我?
我会建议以下内容,尽管它可能缺少您的一些细节,因为我使用 CSS 来设置较小图像 ( .thumbs
) 和主图像 ( #main
的样式):
function show(el) {
if (!el) {
// function requires a reference to the clicked element, here 'el'
return false;
}
else {
// finds the element with id="main"
var showIn = document.getElementById('main'),
// sets the sISrc variable with the src of the showIn element
sISrc = showIn.src,
// sets the variable 'elSrc' with the src value of the clicked element
elSrc = el.src;
// re-sets the src of the 'main' element with the src of the clicked element
showIn.src = elSrc;
// sets the clicked element's src to the (previous) value of the 'main' element
el.src = sISrc;
}
}
// finds all the images
var images = document.getElementsByTagName('img');
for (var i = 0, len = images.length; i < len; i++) {
// iterates through all the images and assigns an 'onclick' event-handler
images[i].onclick = function() {
// passes the current image into the show() function when clicked
show(this);
};
}
JS小提琴演示。
<小时 />编辑了上面的代码,修改了以允许多个实例...图库的事情,这个版本只会影响.main
元素(因为有多个具有相同标识符的元素我已经切换到class
名称,尽管在当前版本中,这要求图像位于同一(即时)父(el.parentNode
):
function show(el) {
if (!el) {
return false;
}
else {
var showIn = el.parentNode.getElementsByClassName('main')[0],
sISrc = showIn.src,
elSrc = el.src;
showIn.src = elSrc;
el.src = sISrc;
}
}
var images = document.getElementsByTagName('img');
for (var i = 0, len = images.length; i < len; i++) {
images[i].onclick = function() {
show(this);
};
}
JS小提琴演示。
引用:
-
document.getElementById()
. -
document.getElementsByTagName()
.
function openimage(mainimageurl)
{
document.getElementById('Idofthebimage').src =mainimageurl;
//set the width height once in css
}
现在说在每个图像上写这个onclick="openimage(this.src)"
或者更大的图像URL是否不同onclick="openimage('theurlofthebiggerimage')"
我整理了以下演示,展示了如何拥有一系列图像元素并将它们与组顶部的主要元素交换。
我还包含了window.onload
事件处理程序,因为在解析元素并将其添加到 DOM 之前,您无法附加元素事件处理程序,这(在大多数情况下)有助于防止 Javascript 中发生的错误。
这不是处理与在页面加载之前或期间运行脚本相关的问题的唯一方法(例如,脚本调用以内联方式运行并访问在window.onload
触发之前已解析的元素)。但对于更小、更程序化的脚本来说,这是一种常见的、大多是持久的解决方案。
<div id="imgs">
<div>
<img id="mainimg" src="http://goo.gl/UohAz"/>
</div>
<img src="http://www.gravatar.com/avatar/f893555be713a24dee52230712cdde3a?s=128&d=identicon&r=PG"/>
<img src="http://www.gravatar.com/avatar/495d675e3bc42ed1dee469e2ce701f1b?s=128&d=identicon&r=PG"/>
<img src="http://www.gravatar.com/avatar/db5a61382c2ba7309071c323edb4013b?s=128&d=identicon&r=PG"/>
</div>
window.onload = function() {
var imgs = document.getElementById('imgs').getElementsByTagName('img'),
mainimg = document.getElementById('mainimg'),
i = imgs.length,
lastimg;
var swap = function() {
var old = mainimg.src;
mainimg.src = this.src;
this.src = old;
};
while (i--) {
img = imgs[i];
img.id = 'img-' + i;
if (img.parentNode.id == 'main-img') {
lastimg = img.id;
}
img.onclick = swap;
}
};
http://jsfiddle.net/userdude/yq9Fq/
- 如何使用JavaScript/jQuery选择图像的多边形区域
- jQuery blimp文件上传-上传后保留选择图像行
- 如何在Imgareaselect中选择图像根据像素进行选择
- 如何在选择图像时显示刻度图像
- jquery选择图像不起作用
- 悬停并选择图像贴图的状态
- 如何在选择图像时显示图像
- 在 JavaScript 中的 ClickEvent 上选择图像
- 响应式文件管理器.防止在选择图像时模式关闭
- 将鼠标悬停在地图上时,取消选择图像映射的选定区域
- 建议为选择图像创建代码
- 选择图像(但尚未上传)时更改输入类
- CKFinder 如何在选择图像时获取尺寸 URL 和尺寸(宽度/高度)(文件:选择)
- 通过 jscript 随机选择图像时动态全尺寸调整图像大小的问题
- html5选择图像并进行操作
- DNN:HTML编辑器不显示要选择图像的文件夹
- 选择图像的一部分,并使用jQuery检索其坐标
- 如何在单击时仅选择图像的一部分
- 在javascript中选择图像
- 从弹出窗口中选择图像并显示在主页面中