可能重复:
使用JavaScript更改元素的CSS类
我正在尝试使用此脚本设置div的属性.我在第三行有问题,我试图从父div获取div-s,id为"loading".但在这里的问题,似乎在变量divs中没有什么.为什么?
脚本:
function blink() {
document.getElementById("loading").setAttribute("class","loader");
var divs = document.getElementById("loading").getElementsByTagName("div");
alert(divs[0].class);
for(var i=0;i<divs.length;i++) {
var _id = divs[i].id;
document.getElementById(divs[i].id).setAttribute("class", "bar");
}
}
Run Code Online (Sandbox Code Playgroud)
HTML:
<div id="loading" class="loader2">
<a href="#" onClick="blink()">
<div class="bar_"></div>
<div class="bar_"></div>
<div class="bar_"></div></a>
</div>
Run Code Online (Sandbox Code Playgroud)
我想替换divs类:"bar_"到"bar".这就是我想要做的.
我有这样的变量:
self.lamp_1
self.lamp_2
self.lamp_3
self.lamp_4
Run Code Online (Sandbox Code Playgroud)
现在我想使用这个名称中的每一个循环来自动调用它们,如下所示:
for i in range(1,5):
self.canvas.itemconfig(self.lamp_/number_i_automaticly/, fill=self.color_blink)
Run Code Online (Sandbox Code Playgroud)
我尝试使用函数eval()但它不起作用.它停止运行我的程序.
eval("self.canvas.itemconfig(self.lamp_"+str(i)+",fill=self.color_blink)")
Run Code Online (Sandbox Code Playgroud)
如何使用密钥i以这种方式解析变量名称?
我有关于unittest的问题.如何进行测试以查看是否存在异常?一个例子:
基准(3,32,2012)
如果我像这样调用类Datum,其中月份不在范围内(> 31),它就是一切正常,它会抛出异常并且没关系.但是如果Exception正常,我想做一个单元测试,如果正在捕获异常确定..?我做了一些测试,但这些只有True值,而且没关系.我不知道如何以这种方式进行测试..并在互联网上搜索..感谢您的回复.
import date,datetime
class Datum():
def __init__(self,day,month,year):
try:
d=int(day)
dvm=stevilodnivmesecu(month,year)
if (d>=1 and d<=dvm):
self.d=d
else:
raise Exception("Day out of moth range")
except:
raise ValueError("Day is not a number")
try:
m=int(month)
if m>=1 and m<=12:
self.m=m
else:
raise Exception("Month out of range")
except:
raise ValueError("Month is not a number")
try:
l=int(year)
if l>=1000 and l<=9999:
self.l=l
else:
raise Exception("Year is out of range")
except:
raise ValueError("Year is not a number")
def __repr__(self):
return repr(self.d)+"."+repr(self.m)+"."+repr(self.l)
def monthrange(month,year):
if …Run Code Online (Sandbox Code Playgroud) python ×2
assert ×1
eval ×1
html ×1
javascript ×1
replace ×1
testing ×1
unit-testing ×1
variables ×1