简体   繁体   English

为什么我的jquery中的if语句无法检测图像大小?

[英]Why can't my if statement in jquery detect image size?

The If - Else statement in my code below seems to be causing an error. 我下面的代码中的If-Else语句似乎引起错误。 When the image is clicked, I'm trying to have a google window open if the image is 100px by 100px in size, and a yahoo window open in any other circumstance. 单击图像时,如果图像的大小为100px x 100px,则尝试打开google窗口,而在其他情况下,则打开yahoo窗口。 Any idea what's making the if statement error out? 知道是什么使if语句出错吗?

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
<script type="text/javascript">


$(document).ready(function() {
$("#testImgID").click(function(){

   var imgWidth = $(this).width();
   var imgHeight = $(this).height();

   if( imgWidth == 100 && imgHeight == 100)
   {
        window.open("http://www.google.com");
   else
   {
        window.open("http://www.yahoo.com");
   }
});
});

<p>
<img width="100" height="100" id="testImgID" alt="Setup client email capture.jpg" src="/wg/PayrollSolutions/PublishingImages/RCP_setups_theatrical_assignedTo_table.png" style="margin: 5px"/>
</p>

You have a syntax error, you didn't close the first if 您有语法错误,如果没有关闭第一个

$(document).ready(function() {
    $("#testImgID").click(function() {

        var imgWidth = $(this).width();
        var imgHeight = $(this).height();

        if (imgWidth == 100 && imgHeight == 100) {
            window.open("http://www.google.com");
        } else { // } was missing
            window.open("http://www.yahoo.com");
        }
    });
});

Always check the developer's console (f12 usually) in order to check for syntax errors, you should see: Uncaught SyntaxError: Unexpected token else and in which line the error is. 始终检查开发人员的控制台(通常为f12)以检查语法错误,您应该看到: Uncaught SyntaxError: Unexpected token else以及错误所在的行。

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

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