admin管理员组

文章数量:1404927

I'm trying to get the example code in this SO question running on my system

getting the X/Y coordinates of a mouse click on an image with jQuery

It displays the position of the mouse on a click inside a div. It works fine on Firefox and Safari but doesn't update on Chrome and I'm wondering if I've done something wrong. I'm using Google Chrome Version 23.0.1271.101

Here's the code I'm using.

<html>

<head>
<script class="jsbin" src=".9.0/jquery.min.js"></script>
</head>

<body>
<p>Click to see the position!</p>
<div class="box" style="width: 200px; height: 200px; background: #F00">&nbsp;</div>
<p id="position">Position will go here</p>
<script>
$(document).ready(function() {
    $('.box').click(function(e) {
        var offset = $(this).offset();
        $('#position').html(Math.round(e.clientX - offset.left) + ", " + Math.round(e.clientY - offset.top));
    });
});
</script>

</body>
</html>

Update: Here is the error I see on Chrome's JavaScript console Uncaught ReferenceError: $ is not defined but I'm not sure what to make of it.

Update2:

I cleared the browser cache and reloaded the page while having the JavaScript console open and noticed an error I had not seen before

[blocked] The page at https://<mywebsite>/index.html ran insecure content from .9.0/jquery.min.js.
Uncaught ReferenceError: $ is not defined 

So, I downloaded jquery.min.js and placed it in the same directory as index.html and changed the loading of the script to

<script src="./jquery.min.js" type="text/javascript"></script>

It now works on all three browsers.... so for some reason Chrome was the only one that plained about the url for jquery.min.js being insecure. To pound the problem, it didn't give me that error when I went into the JavaScript console, and I didn't see it until I had cleared the cache and I had the JavaScript console open.

I'm trying to get the example code in this SO question running on my system

getting the X/Y coordinates of a mouse click on an image with jQuery

It displays the position of the mouse on a click inside a div. It works fine on Firefox and Safari but doesn't update on Chrome and I'm wondering if I've done something wrong. I'm using Google Chrome Version 23.0.1271.101

Here's the code I'm using.

<html>

<head>
<script class="jsbin" src="http://ajax.googleapis./ajax/libs/jquery/1.9.0/jquery.min.js"></script>
</head>

<body>
<p>Click to see the position!</p>
<div class="box" style="width: 200px; height: 200px; background: #F00">&nbsp;</div>
<p id="position">Position will go here</p>
<script>
$(document).ready(function() {
    $('.box').click(function(e) {
        var offset = $(this).offset();
        $('#position').html(Math.round(e.clientX - offset.left) + ", " + Math.round(e.clientY - offset.top));
    });
});
</script>

</body>
</html>

Update: Here is the error I see on Chrome's JavaScript console Uncaught ReferenceError: $ is not defined but I'm not sure what to make of it.

Update2:

I cleared the browser cache and reloaded the page while having the JavaScript console open and noticed an error I had not seen before

[blocked] The page at https://<mywebsite>/index.html ran insecure content from http://ajax.googleapis./ajax/libs/jquery/1.9.0/jquery.min.js.
Uncaught ReferenceError: $ is not defined 

So, I downloaded jquery.min.js and placed it in the same directory as index.html and changed the loading of the script to

<script src="./jquery.min.js" type="text/javascript"></script>

It now works on all three browsers.... so for some reason Chrome was the only one that plained about the url for jquery.min.js being insecure. To pound the problem, it didn't give me that error when I went into the JavaScript console, and I didn't see it until I had cleared the cache and I had the JavaScript console open.

Share Improve this question edited May 23, 2017 at 12:28 CommunityBot 11 silver badge asked Mar 10, 2013 at 22:46 amdnamdn 11.6k36 silver badges45 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 4

The error you are getting is that jQuery is not loaded when executing your script. So therefore $ is not defined when you try to use it.

Also, your screenshot is not showing the same markup as your example.

本文标签: