【发布时间】:2013-03-10 22:46:12
【问题描述】:
我正在尝试让这个 SO 问题中的示例代码在我的系统上运行
getting the X/Y coordinates of a mouse click on an image with jQuery
它显示鼠标在div 内的点击位置。它在Firefox 和Safari 上运行良好,但在Chrome 上没有更新,我想知道我是否做错了什么。
我使用的是 Google Chrome 版本 23.0.1271.101
这是我正在使用的代码。
<html>
<head>
<script class="jsbin" src="http://ajax.googleapis.com/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"> </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>
更新:
这是我在 Chrome 的 JavaScript 控制台 Uncaught ReferenceError: $ is not defined 上看到的错误,但我不知道该怎么做。
更新2:
我在打开 JavaScript 控制台的同时清除了浏览器缓存并重新加载了页面,并注意到一个我以前从未见过的错误
[blocked] The page at https://<mywebsite>/index.html ran insecure content from http://ajax.googleapis.com/ajax/libs/jquery/1.9.0/jquery.min.js.
Uncaught ReferenceError: $ is not defined
所以,我下载了jquery.min.js,并把它和index.html放在同一目录下,并将脚本的加载改为
<script src="./jquery.min.js" type="text/javascript"></script>
它现在可以在所有三种浏览器上运行......所以出于某种原因,Chrome 是唯一一个抱怨 jquery.min.js 的 url 不安全的浏览器。使问题更加复杂的是,当我进入 JavaScript 控制台时,它并没有给我那个错误,直到我清除了缓存 并且我打开了 JavaScript 控制台,我才看到它。
【问题讨论】:
标签: javascript jquery google-chrome firefox safari