最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to change a label value at runtime? - Stack Overflow

programmeradmin5浏览0评论

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.

发布评论

评论列表(0)

  1. 暂无评论