javasctipt 中的backgroundPosition 不起作用
基本上我在 css 中做了翻转,但我想用 javascript 代替。我还希望,一旦我的页面加载,就会自动选择其中一张图片。我尝试了这段代码,但它不起作用......有什么想法吗?由于某种原因它没有调用该函数。 ps:在我的html中,id也被称为clicked3
<script type="text/javascript">
window.onLoad=function(){
clicked3();
}
function clicked3(){
document.getElementById("clicked3").style.backgroundPosition = "-198px top";
}
</script>
Basically I did a rollover in css but i want it in javascript instead. Also I want,once my page is loaded one of the pictures would be automatically selected. I tried this code for that but its not working.. any ideas? for some reason its not calling the function. ps: the id is called clicked3 as well in my html
<script type="text/javascript">
window.onLoad=function(){
clicked3();
}
function clicked3(){
document.getElementById("clicked3").style.backgroundPosition = "-198px top";
}
</script>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
帮自己一个忙,使用 javascript 库,例如 jQuery...
1) Javascript 区分大小写,因此 window.onLoad 与 window.onload 不同(正确的语法)
2) 如果您的图像正好是 198px 宽(你隐藏了图像),你可能忘记在你的CSS中添加
background-repeat:no-repeat;
,让你看到该图像的副本无论如何,改变背景位置的sintax是正确,请在此处查看其实际操作:
http://jsfiddle.net/zszB4/
Do yourself a favor and use a javascript library, such as jQuery...
1) Javascript is case sensitive, so window.onLoad isn't the same as window.onload (correct sintax)
2) If your image is exactly 198px wide (you're hiding the image), you may have forgot to add
background-repeat:no-repeat;
in your css, making you see a copy of that imageAnyway, your sintax for changing background position is correct, see it in action here:
http://jsfiddle.net/zszB4/
您的代码无法正常工作的真正原因是您将函数分配给不存在的事件处理程序
onLoad
。window.onload
是正确的语法,注意它都是小写的。http://jsfiddle.net/9Kh8f/3/
The real reason your code isn't working is because you're assigning your function to a non-existent event handler
onLoad
.window.onload
is the correct syntax, notice it's all lower case.http://jsfiddle.net/9Kh8f/3/
您不需要 window.onload=function.. 您只需调用...
这里为您提供工作演示:)
http:// /jsfiddle.net/9Kh8f/
You dont need the window.onload=function.. you can just call...
Working demo here for you :)
http://jsfiddle.net/9Kh8f/