Javascript onClick“显示:无;” 不起作用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14298462/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 16:18:50  来源:igfitidea点击:

Javascript onClick"display: none;" doesn't work

javascriptjquerycss

提问by gtr123

I have a an image that when you click, this function runs.

我有一张图片,当您单击时,此功能会运行。

function img(num) {
    var src = "images/main" + num + ".jpg";
    document.getElementById("popover-image").src = src;
    $("#sheet").css("display", "block");
}

When you click on the "X" (image), this function runs:

当您单击“X”(图像)时,此函数将运行:

function close() {
    $("#sheet").css("display", "none");
}

but it doesn't work.

但它不起作用。

Here is how my image is set up:

这是我的图像的设置方式:

<img src="images/x.png" alt="Exit" onclick="close()" />

<img src="images/x.png" alt="Exit" onclick="close()" />

回答by wired_in

John Koerner's code:

约翰科纳的代码:

$("#closeButton").click(function () {
    $("#sheet").css("display", "none");
});

Your event handler wasn't being attached properly.

您的事件处理程序未正确附加。

回答by chris

You can do like this also, by using class name

你也可以这样做,通过使用类名

( function($) {
  $(".btn-remove").click(function() {  
    $(this).css("display", "none");      
  });
} ) ( jQuery );

回答by Austin Mullins

We're all a little baffled that you read how to call the jQuery .cssfunction but not .show(), hide(), or attr(). For what it's worth, here's the link to the hide function page.JQuery is the answer. I can't think of a reason not to use it everywhere.

我们都有点莫名其妙,你看如何调用jQuery的.css功能,但没有.show()hide()attr()。对于它的价值,这里是隐藏功能页面的链接。JQuery 就是答案。我想不出不到处使用它的理由。

I think what you're trying to do is:

我认为你想要做的是:

$(document).ready( function() {
  $("img").on("click", function(num) {
     var src = "images/main" + num + ".jpg";
     $("#popover-image").attr("src", src);
     $("#sheet").attr("display", "block");
  });
  $("img[alt=Exit]").on("click", function() {
     $("#sheet").attr("display", "none");
  });
});