Javascript 为什么当提示和确认时,我的盒子的颜色没有改变?
Javascript 为什么当提示和确认时,我的盒子的颜色没有改变?,javascript,html,css,Javascript,Html,Css,函数myFunction(){ 如果(确认(“按下按钮!”)==真){ if(this.style.backgroundColor==“白色”) this.style.backgroundColor=“黄色”; else if(this.style.backgroundColor==“黄色”) this.style.backgroundColor=“红色”; else if(this.style.backgroundColor==“红色”) this.style.backgroundColor=
函数myFunction(){
如果(确认(“按下按钮!”)==真){
if(this.style.backgroundColor==“白色”)
this.style.backgroundColor=“黄色”;
else if(this.style.backgroundColor==“黄色”)
this.style.backgroundColor=“红色”;
else if(this.style.backgroundColor==“红色”)
this.style.backgroundColor=“”
其他的
this.style.backgroundColor=“”;
}否则{
txt=“您按了取消!”;
}
if(confirm('您确定要将此内容保存到数据库中吗?')==true){
//省省吧!
}否则{
//什么都不要做!
}
}
var blocks=document.getElementsByClassName(“foo”);
对于(i=0;i
.foo{
浮动:左;
宽度:30px;
高度:30px;
保证金:5px;
边框:1px实心rgba(0,0,0,2);
边界半径:25%;
}
怀特先生{
背景:#FFFFFF;
}
.全部{
浮动:左;
宽度:900px;
高度:900px;
保证金:5px;
边框:1px实心rgba(0,0,0,2);
}
A1
A2
A3
地下一层
地下二层
地下三层
这对我很有效
说明:
.white
类中读取元素.style
,但当我将内嵌CSS插入到每个元素时,可以读取。这在下文中得到了说明this
作用域,但没有实际提供this实际上是什么。因此,我在click
事件处理程序中传递了this
参数,由于this
不能用作函数参数,因此将其更改为e
函数myFunction(e){
如果(确认(“按下按钮!”)==真){
如果(例如,style.backgroundColor==“白色”)
e、 style.backgroundColor=“黄色”;
else if(e.style.backgroundColor==“黄色”)
e、 style.backgroundColor=“红色”;
否则,如果(e.style.backgroundColor==“红色”)
e、 style.backgroundColor=“白色”
其他的
e、 style.backgroundColor=“白色”;
}否则{
txt=“您按了取消!”;
}
if(confirm('您确定要将此内容保存到数据库中吗?')==true){
//省省吧!
}否则{
//什么都不要做!
}
}
var blocks=document.getElementsByClassName(“foo”);
对于(i=0;i
.foo{
浮动:左;
宽度:30px;
高度:30px;
保证金:5px;
边框:1px实心rgba(0,0,0,2);
边界半径:25%;
}
.全部{
浮动:左;
宽度:900px;
高度:900px;
保证金:5px;
边框:1px实心rgba(0,0,0,2);
}
A1
A2
A3
地下一层
地下二层
地下三层
函数myFunction(e){
如果(确认(“按下按钮!”)==真){
如果(例如,style.backgroundColor==“白色”)
e、 style.backgroundColor=“黄色”;
else if(e.style.backgroundColor==“黄色”)
e、 style.backgroundColor=“红色”;
否则,如果(e.style.backgroundColor==“红色”)
e、 style.backgroundColor=“”
其他的
e、 style.backgroundColor=“”;
}否则{
txt=“您按了取消!”;
}
if(confirm('您确定要将此内容保存到数据库中吗?')==true){
//省省吧!
}否则{
//什么都不要做!
}
}
var blocks=document.getElementsByClassName(“foo”);
对于(i=0;i
.foo{
浮动:左;
宽度:30px;
高度:30px;
保证金:5px;
边框:1px实心rgba(0,0,0,2);
边界半径:25%;
}
怀特先生{
背景色:白色;
}
.全部{
浮动:左;
宽度:900px;
高度:900px;
保证金:5px;
边框:1px实心rgba(0,0,0,2);
}
紫色{
背景:ab3fdd;
}
.葡萄酒{
背景:#ae163e;
}
A1
A2
A3
地下一层
地下二层
地下三层
我不知道你在问什么,也不知道问题出在哪里。单击一个正方形将显示两个提示,并且没有任何框变为黄色。这是一个更好的主意来描述你想要做什么,而不是说“这不起作用”,你正在使用CSS类设置背景色。尝试使用内联样式。这正是我所需要的!请点击我的答案旁边的绿色勾号接受我的答案。不要复制我的答案并重新发布,你应该接受我的答案。我的坏,新到堆栈溢出!我刚刚看到你之前也发布了同样的问题?我创建了另一个问题是为了请求更改颜色的提示,但这篇文章的目的是问为什么颜色没有更改:/,是的,我看到了更改,我不能批准它,因为我的声誉嘿,呃,很抱歉打扰你,但我不能让它在html中工作,我只是复制了它并添加了脚本和样式格式,这对代码影响不大。自从我从代码片段中取出html后,是否还需要向html中添加其他内容?