[英]toggle doesn't work in jQuery
I don't know why this code of mine doesn't work... I want the "+" sign to be shown up and when we click on that sign then to be "-" and at the same time the paragraph be shown up. 我不知道为什么我的代码无法使用...我希望显示“ +”符号,当我们单击该符号时将其显示为“-”,同时显示该段落起来。 When we click the "-" sign to return in the initial state.
当我们单击“-”符号以返回初始状态时。
$(document).ready(function() { $("#main").append("<img src='https://webapps-cdn.esri.com/graphics/ui/plus-sign-10.png' id='clickMe' />"); $("#message").hide(); $("#clickMe").toggle(function() { $("#clickMe").attr("src", "https://webapps-cdn.esri.com/graphics/ui/minus-sign-10.png"); $("#message").show(); }, function() { $("#clickMe").attr("src", "https://webapps-cdn.esri.com/graphics/ui/plus-sign-10.png"); $("#message").hide(); }); });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.0/jquery.min.js"></script> <div id="main"></div> <p id="message">You should see this message!</p>
Try this fiddle . 试试这个小提琴 。
jQuery's toggle
function doesn't handle the click event...you need to use click()
for that. jQuery的
toggle
功能无法处理click事件...您需要为此使用click()
。
var $clickMe = $("#clickMe"),
$message = $("#message");
function toggle() {
if ($message.is(':visible')) {
$clickMe.attr("src","https://webapps-cdn.esri.com/graphics/ui/plus-sign-10.png");
$message.hide();
} else {
$clickMe.attr("src","https://webapps-cdn.esri.com/graphics/ui/minus-sign-10.png");
$message.show();
}
}
$clickMe.click(function(){
toggle();
});
$("#main").append("<img src='https://webapps-cdn.esri.com/graphics/ui/plus-sign-10.png' id='clickMe' />");
$("#message").hide();
function toggle() {
if ($("#message").is(':visible')) {
$("#clickMe").attr("src", "https://webapps-cdn.esri.com/graphics/ui/plus-sign-10.png");
$("#message").hide();
} else {
$("#clickMe").attr("src", "https://webapps-cdn.esri.com/graphics/ui/minus-sign-10.png");
$("#message").show();
}
}
$("#clickMe").click(function() {
toggle();
});
It works! 有用!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.