jQuery .css() 不会更改元素背景颜色
jQuery .css() won't change element background color
我已经用css元素(".cursory")替换了我的鼠标指针。现在它是一个绿色的小圆圈。我设置了一个计时器,可以检测鼠标何时空闲 2 秒。我想在鼠标空闲时将绿色更改为红色,但我无法弄清楚如何让("粗略").css(...) 工作。下面是我的代码,问题.css()在goInactive函数中:
<!DOCTYPE html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script><!--this is the jQuery document link-->
<script>
// this is where jQuery functions go
//TESTING IF THE UI IS IDLE
var TimeoutID;
function inputdetect() {
// attaches event handler to specified event
// takes event as string, function to run, and optional boolean
// to indicate when the event propogates
// these are false, so events "bubble up"
this.addEventListener("mousemove",resetTimer,false);
this.addEventListener("mousedown",resetTimer,false);
this.addEventListener("mousewheel",resetTimer,false);
this.addEventListener("keypress",resetTimer,false);
this.addEventListener("touchmove",resetTimer,false);
this.addEventListener("DOMmousescroll",resetTimer,false);
this.addEventListener("MSpointermove",resetTimer,false);
startTimer();
}
inputdetect();
function startTimer() {
//waits two seconds before calling inactive
TimeoutID = window.setTimeout(goInactive,2000); // does it need to take the window variable??
}
function resetTimer(e) {
window.clearTimeout(TimeoutID);
goActive();
}
function goActive() {
//what happens when the UI is not idle
$('p').text("The UI is not idle.");
startTimer();
}
function goInactive() {
$('p').text("The UI is idle.");
// REPLACING CURSOR WHEN UI IS IDLE
//this part won't work
$('cursory').css("background-color","red");
}
// THIS changes the pointer to a css element
$(document).ready(function() {
$(document).on('mousemove', function(e) {
$('.cursory').css({
left: e.pageX,
top: e.pageY
});
});
});
</script>
</head>
<style>
/*this is where CSS styling goes*/
html {
cursor: none;
}
.cursory {
height: 10px;
width: 10px;
border-radius: 100%;
background-color: green;
position: relative;
}
</style>
<body>
<div class = "cursory"></div>
<!--this is where the HTML will go*/-->
<p>hello</p>
cursory
是元素的类,请使用类选择器.className
$('.cursory').css("background-color","red");
可能缺少问题"."
$('.cursory').css("background-color","red");
一篇关于选择器的小文章:http://www.w3schools.com/jquery/jquery_ref_selectors.asp
根据你上面的代码,看起来你在jQuery调用中没有使用适当的CSS选择器。
更改此内容
$('cursory').css("background-color","red");
自
$('.cursory').css("background-color","red");
相关文章:
- 如何使用css动画/javascript使具有背景图像的元素出现
- 使用li元素的html内容更改该元素的背景
- Javascript获取元素背景图像,但消除了“;url()”;
- 如何获取元素:使用javascript和CORS悬停背景
- "隐藏/显示”;元素和更改按钮背景图像
- 绘制DOM元素:鼠标事件会被背景元素混淆
- 如何在每次页面重新加载时更改渐变背景,同时保持光标位置元素处于活动状态
- Angular Bootstrap$modal与活动背景元素
- 防止鼠标与透明元素背景交互
- 使用 JS 获取 Html 元素背景颜色
- JavaScript 更改元素背景点击调用函数
- 在浏览器中以一致格式获取元素背景颜色的可靠方法
- 如何在单击并悬停时更改td元素背景
- jQuery未更改元素背景颜色
- 从元素背景中抓取图像元素作为DOM资源
- 设置元素背景的alpha值,但不改变其样式.背景值
- Javascript改变元素背景
- jquery-使用elementFromPoint按id更改元素背景颜色
- jQuery .css() 不会更改元素背景颜色
- 创建元素背景图像