admin管理员组

文章数量:1324930

The alert is showing, but the value is not changing.... why?

<html>
    <head>
        <title>Test EuDock</title>
    </head>
    <body >
        <label id="labelID">test</label>
        <script type="text/javascript" >

            document.onkeyup = KeyCheck;

            function KeyCheck(e) {

                var KeyID = (window.event) ? event.keyCode : e.keyCode;

                switch(KeyID)
                {
                    case 39: // right arrow
                        document.getElementById('labelID').value="BLZ";
                        alert('ok');
                        break;
                }

            }
        </script>
    </body>
</html>

The alert is showing, but the value is not changing.... why?

<html>
    <head>
        <title>Test EuDock</title>
    </head>
    <body >
        <label id="labelID">test</label>
        <script type="text/javascript" >

            document.onkeyup = KeyCheck;

            function KeyCheck(e) {

                var KeyID = (window.event) ? event.keyCode : e.keyCode;

                switch(KeyID)
                {
                    case 39: // right arrow
                        document.getElementById('labelID').value="BLZ";
                        alert('ok');
                        break;
                }

            }
        </script>
    </body>
</html>
Share Improve this question asked Sep 17, 2010 at 17:49 StudentStudent 28.4k70 gold badges165 silver badges267 bronze badges
Add a ment  | 

4 Answers 4

Reset to default 7

Only input elements have the property value. You want innerHTML :

document.getElementById('labelID').innerHTML="BLZ";

innerHTML is the only attribute that is supported by all browsers.

innerText is not supported by Firefox and textContent is not supported by <= IE8.

I don't think value is a property defined in the DOM for HTML elements. Try assigning to .innerHTML instead, and I think you'll get the result you want.

Try this instead:

document.getElementById('labelID').innerText ="BLZ";

The label element does not have a value property. use document.getElementById('labelID').innerHTML="BLZ"; instead.

本文标签: javascriptHow to change a label value at runtimeStack Overflow