【发布时间】:2023-03-05 00:39:01
【问题描述】:
我正在尝试掌握 jQuery,但我有一段简单的代码无法正常工作。有人知道为什么这可能不起作用吗?
<html>
<head>
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$("div").live("click", divclicked);
});
function divclicked()
{
if($(this).css("background-color") == "lightblue")
$(this).css("background-color", "red");
}
</script>
<style type="text/css">
div
{
background-color: lightblue;
}
</style>
</head>
<body>
<div id="testdiv">
this is a test
</div>
</body>
</html>
提前感谢您的任何意见。
更新: 下面使用“backgroundColor”而不是“background-color”的解决方案适用于内联样式,而不是使用样式表。
I have updated the example to use a stylesheet.
【问题讨论】: