我有一个包含以下代码的javascript.js文件
$(document).ready(function() {$('#rabbit').click(function (e) {
var offset = $(this).offset(),
left = e.pageX - offset.left,
top = e.pageY - offset.top;
if (top > $(this).height() / 2) {
alertDiv('You\'ve cliked under the middle.', 'alert-white');
} else {
alertDiv('You\'ve cliked above the middle.', 'alert-gray');
}
});
});
function alertDiv(text, cssClass) {
var alrt = $('<div class="alert ' + cssClass + '">' + text + '</div>');
$(document.body).append(alrt);
alrt.click(function () {
alrt.remove();
});
}
在css文件中,我有以下内容
.alert {
position: absolute;
left: 30px;
top: 30px;
width: 300px;
height: 200px;
border: 1px solid black;
}
.alert-white {
background: white;
}
.alert-gray {
background: #ccc;
}
最后,在我的html文件中,我包含以下内容
<LINK href="main.css" rel="stylesheet" type="text/css">
<script src="//ajax.googleapis.com/ajax/libs/jquery/2.0.0/jquery.min.js"></script>
<script src="javascript.js"></script>
最后做这件事
<img src="http://www.clermontanimal.net/images/lop_rabbit_easter.jpg" id="rabbit" alt="" />
我包含了我需要的一切,但当我点击图片时,它仍然不起作用。我从http://jsfiddle.net/htEvT/2/得到了这段代码
我忘了什么?
发布于 2014-06-02 08:53:15
如果您正在使用file:// url,则非协议格式的url将尝试从file://ajax.googleapis.com/ajax/libs/jquery/2.0.0/jquery.min.js
获取该文件。
使用
http://ajax.googleapis.com/ajax/libs/jquery/2.0.0/jquery.min.js
或
而不是https://ajax.googleapis.com/ajax/libs/jquery/2.0.0/jquery.min.js
。
发布于 2014-06-02 18:44:01
尝试在head中包含jquery标记,并在body的底部引用您的脚本。不过,我不确定。您是否检查了控制台错误。引用应该类似于<script src="https:/..."></script>
,因为您给出的是活动的url,而不是本地的
https://stackoverflow.com/questions/23958506
复制相似问题