javascript - 如何在 HTML 中添加 JQuery 代码?

标签 javascript jquery html

我试图用 gif 文件制作鼠标效果,我发现了以下代码:

<html>
<head>
<script  src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js">
</script>
<script>      
$(function(){
    $("body").mousemove(
        function(e){
            $("<img src='http://www.favicon.cc/logo3d/618187.png' />")
            .css({
                'position':'absolute', 
                'top':e.pageY+5,
                'left':e.pageX+-15,
                'width':'30px',
                'height':'30px'
            }).prependTo( $(document.body))
            .fadeOut(100, 'linear', function(){
                $(this).remove(); 
            });
        });
});
</script>
</body>

当我运行 html 文件时,我看不到任何东西,为此,我认为我在代码中犯了一个错误,有人可以帮我修复吗?

最佳答案

将鼠标监听器附加到 $(document)而不是 $('body') .

同样在您的代码中,您缺少结束语 </head>并开始<body>标记(您在选择器中使用的标记)。

演示 here

关于javascript - 如何在 HTML 中添加 JQuery 代码?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18729515/

相关文章:

javascript - 获取当前div内容的百分比

javascript - 基于组件的大型 web 项目的 Backbone-RequireJs 样板

javascript - 如何在替换图像时使用 AJAX 刷新 DIV?

jquery - IE8 z-index 错误

javascript - 如何在 Meteor 中添加/删除用户特定数据

javascript - 为什么没有输出

c# - $(window).load(handler) 不会触发

jquery - 如何使设定的文本大小填充容器

html - 无法使用输入[值 =""] 选择空输入?

html - 悬停一个元素时如何影响其他元素