使用图像 jQuery 切换
toggling with image jquery
当点击add_hide_calculator
时,我希望图像变成calculator_open-img
,当再次点击时,我希望图像变回来。这是我到目前为止所拥有的,我无法在单击时更改图片:
Jquery.js
$('#add_hide_calculator').on("click", function() {
var text = $(this).val();
$("#calculator").toggle();
});
$(window).on("click keydown", function(e) {
//e.preventDefault()
//sets the esc key button
if (e.keyCode === 27 || !$(e.target).is(function() {return $("#calculator, #add_hide_calculator")})) {
$("#calculator").hide()
}
}).focus()
/* toggling image starts here */
$('#add_hide_calculator').on("click", function() {
var text = $(this).text();
if (text == "<img src='calculator_open-img.png' width='200px' height='190px'>") {
$(this).html("<img src='calculator_close-img.png' width='200px' height='190px'>");
} else {
$(this).html("<img src='calculator_open-img.png' width='200px' height='190px'>");
}
});
索引.php
<a id="add_hide_calculator"><img src="calculator_close-img.png" width="200px" height="190px"></a>
您可以检查src
属性值
$('#add_hide_calculator').on("click", function() {
$(this).find('img').attr('src', function(i, src) {
return src == "calculator_open-img.png" ? "calculator_close-img.png" : "calculator_open-img.png";
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<a id="add_hide_calculator">
<img src="calculator_close-img.png" width="200px" height="190px">
</a>
更改此行
var text = $(this).text();
自
var text = $(this).html();
您基本上是在比较锚点(链接)内的 HTML,而不是文本。文本是呈现 html 后最终看到的内容。
相关文章:
- 淡入淡出切换jquery元素
- 如何从左边切换 jquery-ui
- 在两个未知值之间切换 JQuery 动画
- 下滑切换 jquery
- 切换 JQuery 插件
- 简单切换(Jquery)不起作用
- 如何在单击按钮时切换 jquery 数据表的分页
- 切换 jquery 无法正常工作
- 切换 jQuery .animate 函数
- 滑动切换 [jquery 函数] 不起作用
- 安卓键盘似乎切换jquery.slideToggle,任何人都知道修复方法
- 切换Jquery表单Posts
- 单击另一个对象时切换jQuery控件
- 切换jquery读取值错误的复选框
- 追加列表切换(jQuery Mobile)未正确呈现
- 更好的方法来显示两个PHP变量切换jQuery Ajax
- 为什么这个悬停切换jQuery不工作
- 切换Jquery脚本,没有特定的类
- 轻松切换jquery/html/css3在ie中无法正常工作
- 在搜索表单中单击时保持子切换 - jquery