[英]Javascript function(parameter), assign parameter to var
i am failing and i don't understand why, i hope some body could help me out.我失败了,我不明白为什么,我希望有人能帮助我。
<div id="anvil" onmouseover="show(anvil_info)">
what i want to do is when you hover over anvil it starts a function我想要做的是当您将鼠标悬停在铁砧上时,它会启动一个功能
function show(x){
x.style.backgroundImage = "image_"+x
}
My idea was to use the assigned parameter when the function was called to get the image, but if i now look try to use it it doesn't work.我的想法是在调用函数时使用分配的参数来获取图像,但是如果我现在尝试使用它,它不起作用。 How do i use a variable assigned to the function?
如何使用分配给函数的变量?
This one still doens't work:这个还是不行:
function show(x, y){
x.style.zIndex = "1";
x.style.opacity = "1";
y.style.backgroundImage = "url('"+ y + "_hover')"
}
<div id="anvil" onmouseover="show(anvil_info, 'anvil')"></div>
new version :新版本 :
function show(elem, pIdToChange){ document.getElementById(pIdToChange).style.backgroundImage = "url('"+ pIdToChange + "_hover')"; // with color switch document.getElementById(pIdToChange).style.color = "red"; elem.style.color = ""; }
<div id="anvil_info" onmouseover="show(this, 'anvil')" >Hello</div> <div id="anvil" onmouseover="show(this, 'anvil_info')">Goodbye</div>
The best is to use onmouseover="show(this)"
最好是使用
onmouseover="show(this)"
You'll get the element in your function, and then x.style.backgroundImage
will work fine.您将在函数中获取元素,然后
x.style.backgroundImage
将正常工作。
function show(elem, pUrl){ elem.style.backgroundImage = "url('"+ pUrl + "_hover')" alert(pUrl); }
<div id="anvil" onmouseover="show(this, 'anvil')">hello</div>
function show(x, y, z){
y.style.zIndex = "1";
y.style.opacity = "1";
x.style.backgroundImage = "url('"+ z + "_hover.jpg')"
}
<div id="anvil_info" onmouseover="show(anvil, this, 'anvil')" onmouseout="hide(anvil, this, 'anvil')"></div>
<div id="anvil" onmouseover="show(this, anvil_info, 'anvil')" onmouseout="hide(this, anvil_info, 'anvil')"></div>
works great, thanks to blag.效果很好,感谢blag。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.