Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/402.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
试图在javascript中更改光标_Javascript_Html_Css_Png - Fatal编程技术网

试图在javascript中更改光标

试图在javascript中更改光标,javascript,html,css,png,Javascript,Html,Css,Png,我想要javascript中的一个函数,当单击按钮时,它可以将光标更改为png图像。这是我在html和javascript中使用的代码,但显然它不起作用,有人能帮我吗 <form> <input type="button" onclick="changeCursor()" value="click here" /> </form> 单击按钮时,添加光标属性 var btn=document.get

我想要javascript中的一个函数,当单击按钮时,它可以将光标更改为png图像。这是我在html和javascript中使用的代码,但显然它不起作用,有人能帮我吗

<form>
    <input type="button"
           onclick="changeCursor()"
           value="click here"
    />
</form>

单击按钮时,添加
光标
属性

var btn=document.getElementById('btn');
btn.addEventListener('click',function(){
document.body.style.cursor='url(http://dummyimage.com/40x40/000/fff),自动;
})
html,正文{
宽度:100%;
身高:100%;
背景颜色:珊瑚;
保证金:0自动;
}

如果您愿意,也可以使用JQuery:

HTML:

JQuery:

$(document).ready(function(){
    $('input').click(function(){
        $('body').addClass('newcursor');
    });        
});

示例:

您可以通过将CSS设置为
光标:url(myImage.png),auto
来影响光标。您可以将其分配给
document.body.style
,但如果不小心,就会覆盖其中的任何内容。更好的方法是将其分配给CSS类,并将该类附加到
元素。

没有
游标这样的东西。如果你想更改光标,你需要使用CSS,而不是JavaScript。但是我如何在我的JavaScript中更改CSS“当点击按钮时”,而不是悬停。。。我希望该按钮能使光标永久成为我的图像,而不仅仅是当光标悬停在上面时,你能告诉我怎么做吗?@chipChocolate.py你只是将光标应用于
div
,我想他希望它应用于所有东西。可能
document.body.style.cursor=url(“”)
会更好吗?这真是让人困惑,当我在这里运行代码片段时,它工作得很好,但是当我将代码复制粘贴到我的文本编辑器中时,它什么也没做,当我单击按钮时,没有抛出错误…等等,不是代码出错,出于某种原因,我的png文件无法工作。只有某些图像才能成为光标吗?
<form>
    <input type="button" value="click here" />
</form>
body.newcursor
{
    cursor: url(http://media.tumblr.com/tumblr_lqs4qoKE1V1qfoi4t.png), auto;
}
$(document).ready(function(){
    $('input').click(function(){
        $('body').addClass('newcursor');
    });        
});