1

I've been having a problem for a while now and I can't seem to understand how to fix it and I don't know what I'm doing wrong.

What I have to do is when I click on a link on the website it needs to pop up a calculator and then when I click the off button on that calculator it should disappear again. What am I doing wrong!?!? (Note: calcButton is a link and buttonOff is a button)

function showCalc()
{
    alert("button was clicked");
    document.getElementById('calculator').style.visibility = "visible";
}

function hideCalc()
{
    alert("off button was clicked");
    document.getElementById('calculator').style.visibility = "hidden";
}

function main() {
    //store top and left values of calculator
    calculator.style.top = getStyle(calculator, "top");
    calculator.style.left = getStyle(calculator, "left");

    document.getElementById('calcButton').onclick = showCalc;
    document.getElementById('buttonOff').onclick = hideCalc;

}

window.onload = main;

EDIT:

Here's the get style method:

function getStyle(object,styleName)
{
    if (window.getComputedStyle)
    {
        return document.defaultView.getComputedStyle(object, null).getPropertyValue(styleName);
    }
    else if (object.currentStyle)
    {
        return object.currentStyle[styleName];
    }
}

EDIT2:

Calc button (which is an href)

<a href="" id= "calcButton">Calculator</a> 

Also I don't think some people understand what the problem is. The problem is when I click it the calculator turns visible and then back hidden almost instantaneously

4

1 に答える 1

1
<a href="" id= "calcButton">Calculator</a>

これにより、リンクが実際にページをリロードするため、ページがリロードされているため、電卓が表示されて「すぐに隠れる」ように見えます。

hrefプロパティに何かを追加します。

<a href="javascript:void();" id= "calcButton">Calculator</a>

ライブデモ

于 2013-02-22T04:19:51.623 に答える