如何在javascript中切换图像位置
How to toggle image location in javascript?
我想让它,如果你点击一个图像,它改变它的来源,如果你再次点击它,它改变回来。
<img
src="http://images.clipartpanda.com/laughing-smiley-face-clip-art-smiley-face-clip-art10.jpeg"
longdesc="http://smileyfaceplace.weebly.com/uploads/6/9/7/1/6971266/9884860_orig.jpg"
width="400"
id="smiley"
onClick = "toggleHide('smiley');"
/>
脚本:
function toggleHide(ImgId) {
var image = document.getElementById(ImgId).src;
document.getElementById(ImgId).src = document.getElementById(ImgId).longdesc;
document.getElementById(ImgId).longdesc = image;
alert(document.getElementById(ImgId).longdesc);
}
我做错了吗?当我运行下面的代码时,交换确实可以工作,但是替代图像显示为未定义。
我使用longdesc属性,因为它看起来是良性的,是一个有效的属性,但如果可能的话,我更愿意使用我自己的make属性。
编辑:谢谢伙计,它现在工作了:
<img
src="http://images.clipartpanda.com/laughing-smiley-face-clip-art-smiley-face-clip-art10.jpeg"
altsrc="http://smileyfaceplace.weebly.com/uploads/6/9/7/1/6971266/9884860_orig.jpg"
width="400"
id="smiley"
onClick = "toggleHide('smiley');"
/>
脚本:
function toggleHide(ImgId){
var image = document.getElementById(ImgId);
var oldsrc = image.src;
image.src = image.getAttribute('altsrc');
image.setAttribute('altsrc', oldsrc);
//alert(document.getElementById(ImgId).longdesc);
}
longdesc
属性需要为camelcase:
function toggleHide(ImgId){
var image = document.getElementById(ImgId).src;
document.getElementById(ImgId).src = document.getElementById(ImgId).longDesc;
document.getElementById(ImgId).longDesc = image;
//alert(document.getElementById(ImgId).longdesc);
}
<img
src="http://images.clipartpanda.com/laughing-smiley-face-clip-art-smiley-face-clip-art10.jpeg"
longdesc="http://smileyfaceplace.weebly.com/uploads/6/9/7/1/6971266/9884860_orig.jpg"
width="400"
id="smiley"
onClick = "toggleHide('smiley');"
/>
更好的方法
还是那句话,最好使用自定义的data-
属性:
function toggleHide(ImgId) {
var img = document.getElementById(ImgId);
var tmp = img.src;
img.src = img.getAttribute('data-alt');
img.setAttribute('data-alt', tmp);
}
<img src="http://images.clipartpanda.com/laughing-smiley-face-clip-art-smiley-face-clip-art10.jpeg"
data-alt="http://smileyfaceplace.weebly.com/uploads/6/9/7/1/6971266/9884860_orig.jpg"
width="400" id="smiley" onClick="toggleHide('smiley');" />
因为img
没有称为longdesc
的属性,您需要将其作为属性读取。
也可以将图像元素引用传递给toggleHide
,这样就可以避免多次调用document.getElementById()
function toggleHide(el) {
var image = el.src;
el.src = el.getAttribute('longdesc');
el.setAttribute('longdesc', image);
}
<img src="http://images.clipartpanda.com/laughing-smiley-face-clip-art-smiley-face-clip-art10.jpeg" longdesc="http://smileyfaceplace.weebly.com/uploads/6/9/7/1/6971266/9884860_orig.jpg" width="400" id="smiley" onClick="toggleHide(this);" />
注意:即使你已经标记它使用jQuery,因为你的解决方案没有使用jQuery,我也没有使用jQuery。
jquery的解决方案将是
jQuery(function($) {
$('.toggle-hide').click(function() {
var image = this.src,
$this = $(this);
this.src = $this.data('toggle-src');
$this.data('toggle-src', image);
})
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<img src="http://images.clipartpanda.com/laughing-smiley-face-clip-art-smiley-face-clip-art10.jpeg" data-toggle-src="http://smileyfaceplace.weebly.com/uploads/6/9/7/1/6971266/9884860_orig.jpg" width="400" class="toggle-hide" />
相关文章:
- javascript背景图像位置循环
- 使用JavaScript功能切换图像位置
- 视差'背景图像/位置/附件'jQuery的动画很不稳定
- 通过图像计数计算图像位置
- 如何在jquery中使用图像ID获取图像位置
- 在 HTML5 画布中,图像位置在设备之间更改
- 根据图像位置目录附加 CSS 样式
- 似乎无法使用关键事件更改图像位置
- 使用图形 API 检索照片的图像位置
- 具有自定义图像位置的全屏背景幻灯片(每张幻灯片)
- 使图像(位置:固定)仅在DIV内部可见
- 基于鼠标单击坐标显示图像位置
- 如何使用javascript更改网站上的背景图像位置
- 用javascript设置图像位置,不总是正确放置
- Javascript图像位置
- 如何在点击按钮时实现背景图像位置变化的幻灯片效果
- Javascript光标图像位置
- 链接或按钮鼠标悬停弹出框/图像位置更改
- 刷新页面后图像位置不正确
- 使用绝对图像位置