图像切换按钮仅可使用一次 [英] Image toggle button works only once
问题描述
我有一个图像按钮,我正在尝试切换.但是,刷新时仅切换一次.当我单击图像时,它应该更改为第二张图片,并且在下次单击时,更改回原始图片.通过此代码,我可以从第一张图片中获得第二张图片,但是当我再次单击时无法获得原始图片.有人可以指出我要去哪里了吗?这是我正在使用的整个脚本
I have an image button, which I am trying to toggle. However, it toggles only once on refresh. When i click on the image, it should change to the second picture, and on the next click, change back to the oroginal picture. Through this code, I can get to the second picture from the first, but I can't get the original picture back when I click again. Can someone point out where I am going wrong? This is the whole script that I am using
HTML& jQuery-
HTML & jQuery -
<a href='#'>
<input type="image" src="/images/pulpit.jpg" id="btn-true" />
</a>
<script type="text/javascript">
$('#btn-true').click( function () {
if ($(this).prop("src", "/images/pulpit.jpg"))
$(this).prop("src", "smiley.gif");
else if ($(this).prop("src", "smiley.gif"))
$(this).prop("src", "/images/pulpit.jpg");
});
</script>
推荐答案
您的条件语句有点错误,您实际上是在检查中分配了源,请更改行:
Your conditional statements are a little wrong, you're actually assigning a source in your check, change the lines:
if ($(this).prop("src", "/images/pulpit.jpg"))
else if ($(this).prop("src", "smiley.gif"))
收件人:
if ($(this).prop("src") == "/images/pulpit.jpg")
else if ($(this).prop("src") == "smiley.gif")
这篇关于图像切换按钮仅可使用一次的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!