Браузер говорит, что функция не определена (но она есть) - PullRequest
2 голосов
/ 19 апреля 2011

Что не так с этим кодом? HTML-код проверяется, но JavaScript все равно не будет работать. Говорит, что при нажатии кнопки тайм-тайм не определен.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />


<style type="text/css">
.numBtn { width: 100px;}
#microwave_oven{ position: relative;}
#microwave_screen img{ visibility: hidden;}
#microwave_screen{position: absolute; top: 100px; left: 100px;}

#keypad{position: absolute; top: 0px; left: 750px; width: 300px;}
#screen {position: absolute; top: 0px; left: 70px;}
#buttons {position: absolute; top: 50px; left: 0px;}
</style>

<title>Microwave Oven</title>

<script type="javascript">
<!--
//Variables
var timestring = ''; 

function timekey(numkey) 
    {   

    if  (timestring.length >4) 
    {

    }

    else
    {
        timestring += numkey;
        dispTime();

    }   

    }

function dispTime()
{
    //This to display the number
    document.getElementById("timekey").value=timestring;


}
//-->
</script>
</head>
<body>
<div id="microwave_oven">
    <!--This is for the entire microwave-->
    <span id="microwave_back">
    <img src="final_micro.jpg" alt="final micro"/>
    </span>

    <!--Screen where the light up image goes and it will be hidden-->
    <span id="microwave_screen">
    <!--This is where screen would light up while i(n use-->
    <img src="window.jpg" id="scrnimg" alt="window" />
    </span>

    <div id="keypad">

        <!--This is the textbox where output of numbers are displayed-->

        <div id="screen">
        <input type="text" id="textbox" />
        </div>

        <div id="buttons">

            <table>


                <tr><td><input type="button" id="popcorn" class="numBtn" value="Popcorn" onclick="timekey('50')" /></td>
                <td><input type="button" id="poultry" class="numBtn" value="Poultry" onclick="timekey('50')" /></td>
                <td><input type="button" id="pizza" class="numBtn" value="Pizza" onclick="timekey('50')" /></td></tr>

                <tr><td><input type="button" id="frozen_entree" class="numBtn" value="Frozen entree" onclick="timekey('50')" /></td>
                <td><input type="button" id="baked_goods" class="numBtn" value="Baked goods" onclick="timekey('50')" /></td>
                <td><input type="button" id="beverage" class="numBtn" value="Beverage" onclick="timekey('50')"/></td></tr>



                <tr><td><input type="button" id="i" class="numBtn" value="1" onclick="timekey('1')" /></td>
                <td><input type="button" id="ii" class="numBtn" value="2"  onclick="timekey('2')" /></td>
                <td><input type="button" id="iii" class="numBtn" value="3" onclick="timekey('3')" /></td></tr>

                <tr><td><input type="button" id="iv" class="numBtn" value="4" onclick="timekey('4')" /></td>
                <td><input type="button" id="v" class="numBtn" value="5" onclick="timekey('5')" /></td>
                <td><input type="button" id="vi" class="numBtn" value="6" onclick="timekey('6')" /></td></tr>

                <tr><td><input type="button" id="vii" class="numBtn" value="7" onclick="timekey('7')" /></td>
                <td><input type="button" id="viii" class="numBtn" value="8" onclick="timekey('8')" /></td>
                <td><input type="button" id="ix" class="numBtn" value="9" onclick="timekey('9')" /></td></tr>

                <tr><td><input type="button" id="clear" class="numBtn" value="Stop/Clear" onclick="timekey('50')" /></td>
                <td><input type="button" id="zero" class="numBtn" value="0" onclick="timekey('0')" /></td>
                <td><input type="button" id="Start" class="numBtn" value="Start" onclick="timekey('50')"/></td></tr>




            </table>

        </div>

    </div>
</div>
</body>
</html>

Uncaught ReferenceError: временная клавиша не определена (анонимная функция) dreamweaver.html: 87 onclickdreamweaver.html: 88

Ответы [ 5 ]

5 голосов
/ 19 апреля 2011

ошибка в типе скрипта:

<script type="text/javascript">
1 голос
/ 19 апреля 2011

Не причина вашей проблемы, но она может вызвать проблемы позже, поэтому стоит указать:

Ваш метод dispTime выполняет getElementById ("timekey"), но в вашем HTML нет элемента HTML с идентификатором timekey.

Как вы, наверное, хотели:

<div id="screen">
<input type="text" id="textbox" />
</div>

быть:

<div id="screen">
<input type="text" id="timekey" />
</div>
1 голос
/ 19 апреля 2011

попробуйте это>

var timekey = function (numkey) 
    {   

    if  (timestring.length >4) 
    {

    }

    else
    {
        timestring += numkey;
        dispTime();

    }   

    }
1 голос
/ 19 апреля 2011

Посмотрите на эти строки в вашем коде.Вы закомментировали весь ваш скрипт.

<script type="javascript">
<!--

  ...
//-->

Дмитрий прав, ваш тип скрипта неточный, он должен быть type = "text / javascript"

1 голос
/ 19 апреля 2011

Вам нужно дождаться загрузки всего DOM.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...