Javascript 如何更改Mousedown?或任何其他事件上Div的样式…;
但它不起作用。您可以看到,我正在尝试更改包含seasure.png onmousedown的div的类,并将其恢复为onmousedup 我错过了什么 谢谢。Javascript 如何更改Mousedown?或任何其他事件上Div的样式…;,javascript,html,css,Javascript,Html,Css,但它不起作用。您可以看到,我正在尝试更改包含seasure.png onmousedown的div的类,并将其恢复为onmousedup 我错过了什么 谢谢。 迈克一切正常。你发布的代码没有问题,所以如果你看不到它,你的css肯定有问题 我用它来测试代码: <html> <head> <title>Test</title> <style> .winter { border: 1px solid blue; } .spring { ba
迈克一切正常。你发布的代码没有问题,所以如果你看不到它,你的css肯定有问题 我用它来测试代码:
<html>
<head>
<title>Test</title>
<style>
.winter { border: 1px solid blue; }
.spring { background: yellow; }
.summer { background: green; }
</style>
</head>
<body>
<div class="winter spring" onmousedown="this.className='winter summer'" onmouseup="this.className='winter spring'">
<img src="Resources/season.png" />
</div>
</body>
</html>
试验
.winter{边框:1px纯蓝色;}
.spring{背景:黄色;}
.summer{背景:绿色;}
它工作得很好。你发布的代码没有问题,所以如果你看不到它,你的css肯定有问题
我用它来测试代码:
<html>
<head>
<title>Test</title>
<style>
.winter { border: 1px solid blue; }
.spring { background: yellow; }
.summer { background: green; }
</style>
</head>
<body>
<div class="winter spring" onmousedown="this.className='winter summer'" onmouseup="this.className='winter spring'">
<img src="Resources/season.png" />
</div>
</body>
</html>
试验
.winter{边框:1px纯蓝色;}
.spring{背景:黄色;}
.summer{背景:绿色;}
使用以下工具非常简单:
这非常容易:
它对我有效,所以我想你需要对你的代码进行一些检查。确保包含您的css 发布您正在使用的完整代码,以便我们可以查找错误 这是我使用的代码:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>onmouseevents on div</title>
<style type="text/css">
body
{
background-color: #FFF;
color: #000;
}
.page
{
margin: 10px auto;
width: 640px;
}
.winter
{
background-color: cyan;
}
.spring
{
color: magenta;
}
.summer
{
color: yellow;
}
</style>
</head>
<body>
<div class="page">
<div class="winter spring" onmousedown="this.className='winter summer'" onmouseup="this.className='winter spring'">
Cats!
</div>
</div>
</body>
</html>
div上的onmouseevents
身体
{
背景色:#FFF;
颜色:#000;
}
页
{
利润率:10px自动;
宽度:640px;
}
冬天
{
背景色:青色;
}
春天
{
颜色:洋红色;
}
.夏天
{
颜色:黄色;
}
猫!
它对我有效,所以我想你需要对你的代码进行一些检查。确保包含您的css
发布您正在使用的完整代码,以便我们可以查找错误
这是我使用的代码:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>onmouseevents on div</title>
<style type="text/css">
body
{
background-color: #FFF;
color: #000;
}
.page
{
margin: 10px auto;
width: 640px;
}
.winter
{
background-color: cyan;
}
.spring
{
color: magenta;
}
.summer
{
color: yellow;
}
</style>
</head>
<body>
<div class="page">
<div class="winter spring" onmousedown="this.className='winter summer'" onmouseup="this.className='winter spring'">
Cats!
</div>
</div>
</body>
</html>
div上的onmouseevents
身体
{
背景色:#FFF;
颜色:#000;
}
页
{
利润率:10px自动;
宽度:640px;
}
冬天
{
背景色:青色;
}
春天
{
颜色:洋红色;
}
.夏天
{
颜色:黄色;
}
猫!
.summer{背景色:红色;}
.spring{背景色:蓝色;}
.aa{字体大小:18px;}
.bb{字体大小:36px;}
aaaaaaaaa
我测试了这段代码,它运行得很好。
我认为,这不是javascript的问题。尝试检查css或其他内容
也许把js代码和css代码放在img上是你需要的。
.summer{背景色:红色;}
.spring{背景色:蓝色;}
.aa{字体大小:18px;}
.bb{字体大小:36px;}
aaaaaaaaa
我测试了这段代码,它运行得很好。
我认为,这不是javascript的问题。尝试检查css或其他内容
也许将js代码和css代码放在img上是您需要的。作为替代方法-我使用了悬停伪选择器 下面是一个个人例子:
.sidebar ul.sidebuttons li a
{
font: bold italic x-large/1.1 trebuchet ms,verdana,sans-serif;
display: block; /* effect should be in a new box not inline */
text-decoration: none; /* turn off link underlining */
color: yellow;
background: black url(sidebar_off.png) no-repeat center;
padding: 10px 10px 10px 5px;
margin: 0px 0px 20px 0px;
border: white outset;
border-width: 2px 2px 2px 0px;
text-align: right;
text-transform: lowercase;
}
.sidebar ul.sidebuttons li a:hover
{
background: yellow url(sidebar_on.png) no-repeat center;
color: black;
border: black inset;
text-decoration: none;
border-width: 2px 2px 2px 0px;
}
HTML看起来像:
<div class="sidebar">
<ul class="sidebuttons">
<li><a href="somelink.html" title="A linke">Go Somewhere</a></li>
如果您遇到问题,请尝试Firefox Web Developer插件或类似工具,以检查页面该部分的样式信息。它可能不会触发您认为它应该触发的内容。作为替代方案,我使用了悬停伪选择器 下面是一个个人例子:
.sidebar ul.sidebuttons li a
{
font: bold italic x-large/1.1 trebuchet ms,verdana,sans-serif;
display: block; /* effect should be in a new box not inline */
text-decoration: none; /* turn off link underlining */
color: yellow;
background: black url(sidebar_off.png) no-repeat center;
padding: 10px 10px 10px 5px;
margin: 0px 0px 20px 0px;
border: white outset;
border-width: 2px 2px 2px 0px;
text-align: right;
text-transform: lowercase;
}
.sidebar ul.sidebuttons li a:hover
{
background: yellow url(sidebar_on.png) no-repeat center;
color: black;
border: black inset;
text-decoration: none;
border-width: 2px 2px 2px 0px;
}
HTML看起来像:
<div class="sidebar">
<ul class="sidebuttons">
<li><a href="somelink.html" title="A linke">Go Somewhere</a></li>
如果您遇到问题,请尝试Firefox Web Developer插件或类似工具,以检查页面该部分的样式信息。它可能不会触发您认为应该触发的内容。我使用的Javascript文件是一个非常不灵活的库。我用另一种方法解决了这个问题。我使用的Javascript文件是一个非常不灵活的库。我用另一种方法解决了这个问题。原来的冬春课是否显示了?因为你的名字选择器可能不正确?你能把那部分代码贴出来吗?这是工作代码,你说的“它不工作”是什么意思?首先,感谢大家的帮助。可怕的事实是:这两种样式都不在CSS规则或文件中。我依靠两个非常大的javascript文件(我没有编写)使它们工作。我猜,在这种情况下,根本不可能将类切换到其他类。原始的冬春类是否显示?因为你的名字选择器可能不正确?你能把那部分代码贴出来吗?这是工作代码,你说的“它不工作”是什么意思?首先,感谢大家的帮助。可怕的事实是:这两种样式都不在CSS规则或文件中。我依靠两个非常大的javascript文件(我没有编写)使它们工作。我猜,在这种情况下,根本不可能将类切换到其他类。因为这与问题中的代码做了相同的事情,这也不起作用,因为问题在其他地方……请注意,这实际上比原始代码更详细,许多人都指出这是正确的。OP对JQuery没有兴趣。从什么时候开始,以前对JQuery的兴趣是用JQuery解决方案回答javascript问题的先决条件?我也不同意冗长的说法,因为jQuery解决方案适用于页面中包含“winter”类的所有div。因此,他不需要将所有javascript内联到每个div中。因为这与问题中的代码做了相同的事情,这也不起作用,因为问题出在其他地方……请注意,这实际上比原始代码更详细,许多人都注意到这一点是正确的。OP对JQuery没有兴趣。从什么时候开始,以前对JQuery的兴趣是用JQuery解决方案回答javascript问题的先决条件?我也不同意冗长的说法,因为jQuery解决方案适用于页面中包含“winter”类的所有div。因此,他不需要将所有javascript嵌入到每个div中。