简体   繁体   English

show() 和 toggle() 没有按预期工作

[英]show() and toggle() not working as expected

b1 hides the image while b2 doesn't show it. b1隐藏图像,而b2不显示它。 Also the toggle function does not work? toggle function 也不起作用? What is wrong in my code?我的代码有什么问题?

$(document).ready(function() {
  $(".b1").click(function() {
    $("img").hide();
  });

  $(".b2").click(function() {
    $("img").show();
  });     
});

Without the code it is impossible to give the right answer.没有代码就不可能给出正确的答案。 If I assume that everything is good with the HTML and CSS, I would do something like this:如果我假设 HTML 和 CSS 一切正常,我会这样做:

$(document).ready(function() {

var imgOne = $(".b1");
var imageTwo = $(".b2");

var imgClass = $(".img-class");


  imgOne.on("click",function() {
    imgClass.hide();
  });

  imageTwo.on("click",function() {
    imgClass.show();
  });     
});

You can also add console.log('pass') when you click to see if the functions are even doing something.您还可以在单击时添加console.log('pass')以查看函数是否正在执行某些操作。 Like this:像这样:

$(document).ready(function() {

var imgOne = $(".b1");
var imageTwo = $(".b2");

var imgClass = $(".img-class");


  imgOne.on("click",function() {
    console.log('pass hide');
    imgClass.hide();
  });

  imageTwo.on("click",function() {
    console.log('pass show');
    imgClass.show();
  });     
});

You should see these prints in the console if everything works ok.如果一切正常,您应该在控制台中看到这些打印。 Try it out.试试看。 Hope this helps:)希望这可以帮助:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM