我需要一种改变html页面鼠标光标的方法。我知道可以使用css来实现,但我需要能够在运行时更改它,例如在页面上有按钮,当它们被点击时,将光标更改为特定的自定义图形。我认为这样做的最好(或唯一?)方法是通过javascript?希望有一种好的方法能够在所有主要浏览器上正常工作。
如果有人能够帮助我,我将非常感激。
提前致谢。
如果有人能够帮助我,我将非常感激。
提前致谢。
感谢回复。 我终于搞定了。以下是我的做法:
<html>
<head>
<script type="text/javascript">
function changeToCursor1(){
document.body.style.cursor="url('cursor1.ani'),url('cursor1.cur'), default";
}
function changeToCursor2(){
document.body.style.cursor="url('cursor2.ani'),url('cursor2.cur'), default";
}
</script>
</head>
<body>
<form>
<input type="button" value="Change to cursor 1" onclick="changeToCursor1()" /><br>
<input type="button" value="Change to cursor 2" onclick="changeToCursor2()" />
</form>
</body>
我发现在Firefox中使其工作,您必须至少传递两个光标选择,例如cursor="url('cursor1.cur'), default",否则它将无法正常工作。此外,在Firefox中,它不支持ani-cursors,只支持cur,这就是为什么我在ani后面放了一个cur的原因。ani将显示在IE中,cur将在Firefox中显示。
有人知道是否可以在IE中更改光标而不会出现active-X警告并要求用户接受吗?
使用 JQuery:
$('.myButton').click(function(){
$(this).css('cursor', 'url(/url/to/cursor/image)');
});
如果你只想在链接上做这件事,那么这很容易。你可以使用以下类似的CSS:
a:hover { cursor: crosshair; } #this is when you mouseover the link
a:active { cursor: wait; } #this is the moment you click it
由于 :active 仅适用于 a 元素,您可能需要使用 Prestaul 和 scunliffe 提出的 Javascript。
function NewPageSize() {
var e=$('#table1-pagesize');
e.attr('disabled', true);
console.log('page-size change event');
$('#time').css('background-color',RGB('#ffff00'));
let mPageSize=e.val();
FillTable(-1, 1, mPageSize); //a function that can take a very long time
e.attr('disabled', false);
$('#time').css('background-color',RGB('#ffffff'));
};
function startWorker() {
if(typeof(w) == "undefined") {
w = new Worker("/js/worker.js");
}
w.onmessage = function(event) {
document.getElementById("time").innerHTML = event.data;
PROC();
};
}
function stopWorker() {
w.terminate();
w = undefined;
}
function PROC() {return;} //empty function
function NewPageSize() {
var e=$("#table1-pagesize");
e.attr('disabled', true);
let mPageSize=e.val();
console.log('page-size change event');
$('#time').css('background-color',RGB('#ffff00'));
gSet('Beta',
`
FillTable(-1, 1,`+mPageSize+`);
$('#time').css('background-color',RGB('#ffffff'));
var e=$("#table1-pagesize");
e.attr('disabled', false);
`
);
PROC=Alfa;
};
function EmptyProc() {return;}
function Alfa() {
PROC=Beta;
}
function Beta() {
PROC=EmptyProc;
let s=gGet('Beta');
let arr=s.split(';');
for (var i=0; i<Len(arr); i++) {
let t=arr[i];
if (Len(Trim(t))>1) {
eval(t);
}
}
}
function NewPageSize() {
var e=$("#table1-pagesize");
e.attr('disabled', true);
let mPageSize=e.val();
console.log('page-size change event');
$('#time').css('background-color',RGB('#ffff00'));
Beta=function () {
PROC=EmptyProc;
FillTable(-1, 1,eval(mPageSize)); //our long running function
$('#time').css('background-color',RGB('#ffffff'));
var e=$("#table1-pagesize");
e.attr('disabled', false);
}
PROC=Alfa;
}
//you can set all the normal cursors like this
someElem.style.cursor = 'progress';
// Get the element you want to change the cursor for
var el = document.getElementById('yourID');
// This image url is relative to the page url
el.style.cursor="url(pathToImage.png)";