通过id获取元素问题?
我试图通过使用带有 onclick
命令的图像来淡入一个框。但我不知道为什么它不起作用。
任何帮助将不胜感激!这是代码:
<style>
article, aside, figure, footer, header, hgroup,
menu, nav, section { display: block; }
#box { background:#999; width:500px; height:500px;opacity:0; }
</style>
</head>
<div id="box"><id="box"></div>
<div id="img"><img src="img.png" width="37" height="28" id="img"></div>
<script>
var elem = document.getElementById("img","box");
// attach event handler
"img".onclick = function(){
fadeIn( "img", 400 );
this.onclick = null;
};
function setOpacity( obj, value ) {
if ( obj ) {
obj.style.opacity = value / 100;
obj.style.filter = 'alpha(opacity=' + value + ')';
obj.style.zoom = 1;
}
}
// makes an element to fade in
function fadeIn( dom, interval, delay ) {
interval = interval || 1000;
delay = delay || 10;
var opacity = 0,
start = Number(new Date()),
op_per_ms = 100 / interval;
if ( typeof dom === "string" ) {
dom = document.getElementById( dom );
}
function step() {
var now = Number(new Date()),
elapsed = now - start;
opacity = elapsed * op_per_ms;
setOpacity( dom, opacity );
if ( elapsed < interval )
setTimeout( step, delay );
else
setOpacity( dom, 100 );
}
setTimeout( step, delay );
}
</script>
I am trying to fade a box in by using an image with a onclick
command. But I am lost as to why it's not working.
Any help would be greatly appreciated! Here is the code:
<style>
article, aside, figure, footer, header, hgroup,
menu, nav, section { display: block; }
#box { background:#999; width:500px; height:500px;opacity:0; }
</style>
</head>
<div id="box"><id="box"></div>
<div id="img"><img src="img.png" width="37" height="28" id="img"></div>
<script>
var elem = document.getElementById("img","box");
// attach event handler
"img".onclick = function(){
fadeIn( "img", 400 );
this.onclick = null;
};
function setOpacity( obj, value ) {
if ( obj ) {
obj.style.opacity = value / 100;
obj.style.filter = 'alpha(opacity=' + value + ')';
obj.style.zoom = 1;
}
}
// makes an element to fade in
function fadeIn( dom, interval, delay ) {
interval = interval || 1000;
delay = delay || 10;
var opacity = 0,
start = Number(new Date()),
op_per_ms = 100 / interval;
if ( typeof dom === "string" ) {
dom = document.getElementById( dom );
}
function step() {
var now = Number(new Date()),
elapsed = now - start;
opacity = elapsed * op_per_ms;
setOpacity( dom, opacity );
if ( elapsed < interval )
setTimeout( step, delay );
else
setOpacity( dom, 100 );
}
setTimeout( step, delay );
}
</script>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
getElementById 没有超过 1 个参数
此外,您的 IE 事件处理程序正在从字符串调用方法。
getElementById does not have more than 1 argument
Also your IE event handler is calling a method from a String.
我认为您在第一段代码中犯了两个错误:
首先:
是错误的,因为函数
getElementById
仅采用一个参数。如果你想获取两个元素,你应该使用两个不同的调用:其次,这是什么?
我认为你的意思是这样的:
第一次更正后变成:
I think you're making two errors within this first piece of code:
First of all:
is wrong because the function
getElementById
just takes one parameter. If you want to get two elements, you should use two different calls:Secondly what's this?
I think you meant this:
That with the first correction becomes: