Javascript函数(参数),为var分配参数

时间:2015-11-15 19:24:28

标签: javascript jquery function-parameter

我失败了,我不明白为什么,我希望有些机构可以帮助我。

$recepies =~ s/
                    (?|
                    ([^\.\/]) ($dimentions) 
                    |
                    (\(?) ($wholeNumberDecimal)     #ex: 1.5
                    |
                    (\(?) ($wholeNumber)            #ex: 1
                    |
                    (\(?) ($wholeNumberFraction) 
                    )
                    (\s) ($unit)

                    /transformer($1,$2,$3,$4) /eixg;    #the replacement

我想要做的是当你将鼠标悬停在铁砧上时,它会启动一个功能

<div id="anvil" onmouseover="show(anvil_info)">

我的想法是在调用函数时使用指定的参数来获取图像,但是如果我现在看起来尝试使用它则不起作用。如何使用分配给函数的变量?

这个仍然不起作用:

function show(x){
x.style.backgroundImage = "image_"+x
}

2 个答案:

答案 0 :(得分:0)

新版本:

 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>

最好使用onmouseover="show(this)"

您将获得函数中的元素,然后x.style.backgroundImage将正常工作。

function show(elem, pUrl){
  elem.style.backgroundImage = "url('"+ pUrl + "_hover')"
  alert(pUrl);
}
<div id="anvil" onmouseover="show(this, 'anvil')">hello</div>

答案 1 :(得分:-1)

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>

效果很好,多亏了blag。