如何在图像单击时调用 jquery 函数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13726404/
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-26 12:54:46  来源:igfitidea点击:

how to call a jquery function on image click

jquery

提问by Priya

I've an image in my JSP page. I need to call the jquery function on click of that image. Please help me !!

我的 JSP 页面中有一个图像。我需要在单击该图像时调用 jquery 函数。请帮我 !!

JSP page look like,

JSP页面的样子,

<html>
    <body>
        <input type="image" id="closeAuction" value="Close Auction" align="right" src="images/close_auction.png" onclick="return endAuction();" disabled / > 
    </body>
</html>

My jquery function inside the script tag looks like,

我的脚本标签内的 jquery 函数看起来像,

<script src="JS/jquery-1.3.2.min.js">
    $(document).ready(function() {     
        $('#closeAuction').click(function() {
            location.reload();     
        });    
    });
</script>

But the script doesn't work!! Could anyone help on calling a function on image click..

但是脚本不起作用!!任何人都可以帮助调用图像点击功能..

回答by Selvakumar Arumugam

You are missing a quote.. $('#closeAuction')

你缺少一个报价.. $('#closeAuction')

Change $('#closeAuction)to $('#closeAuction')

更改$('#closeAuction)$('#closeAuction')

Also you need to put it in a new script

你还需要把它放在一个新的脚本中

<script src="JS/jquery-1.3.2.min.js"></script>
<script type="text/javascript"> //Added new `<script>` tag
   $(document).ready(function() {
     //added missing-v quotes 
     $('#closeAuction').click(function() {
        location.reload();     
     });    
   });    
</script>

回答by Rory McCrossan

You're missing a closing quote and you also need to place your code in it's own script block. Try this:

您缺少结束引号,您还需要将代码放在它自己的脚本块中。尝试这个:

<script src="JS/jquery-1.3.2.min.js"></script>
<script type="text/javascript">
    $(document).ready(function() {     
        $('#closeAuction').click(function() { // <-- note closing quote on selector.
            location.reload();     
        });    
    });
</script>

Is there a specific reason you're using such an outdated version of jQuery? You should try upgrading to 1.8.3.

您使用如此过时的 jQuery 版本是否有特定原因?您应该尝试升级到 1.8.3。

回答by Sridhar Narasimhan

Replace $('#closeAuction)with $('#closeAuction')

替换$('#closeAuction)$('#closeAuction')

Thanks

谢谢

回答by Gabriele Petrioli

You cannot put code inside a scripttag that is used to load an external resource.

您不能将代码放入script用于加载外部资源的标签中。

<script src="JS/jquery-1.3.2.min.js"></script>
<script>
$(document).ready(function() {     
    $('#closeAuction').click(function() {
        location.reload();     
    });    
});
</script>