简体   繁体   English

未捕获的ReferenceError :(函数)未在HTMLButtonElement.onclick中定义

[英]Uncaught ReferenceError: (function) not defined at HTMLButtonElement.onclick

The following functions, onColourChange and onWindowChange , should cycle through images on button click. 以下函数onColourChangeonWindowChange应在按钮单击时循环显示图像。 Instead, I get both: 相反,我得到两个:

Uncaught ReferenceError: onColourChange is not defined at HTMLButtonElement.onclick 未捕获的ReferenceError:未在HTMLButtonElement.onclick中定义onColourChange

and

Uncaught ReferenceError: onWindowChange is not defined at HTMLButtonElement.onclick 未捕获的ReferenceError:未在HTMLButtonElement.onclick上定义onWindowChange

HTML: HTML:

    <head>
    <title>TravelSmart</title>
    <link rel="stylesheet" type="text/css" href="../CSS/style.css">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <link rel="icon" href="../IMAGES/TravelSmart.ico">
    <script type="text/javascript" language="javascript" src="../JS/bmwcustom.js"></script>
</head>

<body>
<div id=wrapper> 
    <header>
        <img class="logo" src="../IMAGES/TravelSmart.png">
    </header>

    <nav>   
        <ul class="navbar">
            <li><a class="active" href="../HTML/index.htm">Home</a></li>
            <li><a href="../HTML/News.html">News</a></li>
            <li><a href="../HTML/Products.html">Products</a></li>
            <li><a href="../HTML/OpeningHours.html">Opening Hours</a></li>
            <li><a href="../HTML/Location.html">Location</a></li>
            <li><a href="../HTML/Offers.html">Offers</a></li>
        </ul>
    </nav>

<main>
    <div class="customcontainer">
        <div class="colour">
            <img id="colour-image" src="../IMAGES/o"/>
            <button class="left-button" onclick="onColourChange(+1)">&laquo;</button>
            <button class="right-button" onclick="onColourChange(+1)">&raquo;</button>
        </div>

        <div class="windows">
        <img id="windows-image" src="../IMAGES/x"/>
        <button class="left-button" onclick="onWindowChange(+1)">&laquo;</button>
        <button class="right-button" onclick="onWindowChange(+1)">&raquo;</button>
        </div>
    </div>

    <input id="selector-box" type="text" value="h: 0 | t: 0" readonly />

</main>


</div>
</body>

JavaScript: JavaScript的:

//Car Colour Image
var colourImages =
[
    "../IMAGES/windowsred.jpg"
    "../IMAGES/bmwblue.jpg"
    "../IMAGES/bmwgreen.jpg"
];

//Car Window Image
var windowImages =
[
    "../IMAGES/bmwwindowred"
    "../IMAGESbmwwindowblue"
    "../IMAGES/bmwwindowgreen"
];

//Component Index
var colourIndex, windowIndex;

//Default to 0
colourIndex = windowIndex = 0;

//Current Component
var colourImage, windowImage;

//On page load call:
window.onload = function()
{
    //Element for each component
    colourImage = document.getElementById("colour-image");
    windowImage = document.getElementById("windows-image");

    //Set initial
    onCarChanged();
}

//Updated Selector Box with Current Selecton
function updateSelectorBox()
{
    //Get the element to be changed
    var selectorBox = document.getElementById("selector-box");

    //Set the value to 0
    selectorBox.value = "";

    //Append each index to string
    selectorBox.value += ("Colour: " + colourIndex + " | ");
    selectorBox.value += ("Colour: " + windowIndex);
}

//Call when colour is changed
function onColourChange(offset)
{
    //Find the index which is offset from the current head index by the given offset.
    var offsetIndex = (colourIndex + offset);

    //If negative set index to last image
    if(offsetIndex < 0)
        colourIndex = colourImages.length + offset;

    //Otherwise add offset and modulo by the length to wrap around the values.
    else
        colourIndex = (colourIndex + offset) % colourImages.length;

    //Call back when body changes
    onCarChanged();
}

//Call when windows are changed
function onWindowChange(offset)
{
    //Find the index which is offset from the current head index by the given offset.
    var offsetIndex = (windowIndex + offset);

    //If negative set index to last image
    if(offsetIndex < 0)
        windowIndex = windowImages.length + offset;

    //Otherwise add offset and modulo by the length to wrap around the values.
    else
        windowIndex = (windowIndex + offset) % windowImages.length;

    //Call back when body changes
    onCarChanged();
}

//Call when car is changed in some way
function onCarChanged()
{
    updateSelectorBox();

    //Set colour and windows images
    windowImage.src = windowImages[windowIndex];
    colourImage.src = colourImages[colourIndex];
}

//Save to local storage
function saveSelection()
{
    localStorage.setItem("chosenColour" , colourIndex);
    localStorage.setItem("chosenWindows" , windowIndex);
}

function loadSelection()
{
    colourIndex = localStorage.getItem("chosenColour");
    windowIndex = localStorage.getItem("chosenWindows");

    onCarChanged()
}

You have a syntax error in your script and thus the js fails. 您的脚本中存在语法错误,因此js失败。

You have to separate arrays values with commas, ie change 您必须用逗号分隔数组值,即更改

//Car Colour Image
var colourImages =
[
    "../IMAGES/windowsred.jpg"
    "../IMAGES/bmwblue.jpg"
    "../IMAGES/bmwgreen.jpg"
];

//Car Window Image
var windowImages =
[
    "../IMAGES/bmwwindowred"
    "../IMAGESbmwwindowblue"
    "../IMAGES/bmwwindowgreen"
];

to

//Car Colour Image
var colourImages =
[
    "../IMAGES/windowsred.jpg",
    "../IMAGES/bmwblue.jpg",
    "../IMAGES/bmwgreen.jpg"
];

//Car Window Image
var windowImages =
[
    "../IMAGES/bmwwindowred",
    "../IMAGESbmwwindowblue",
    "../IMAGES/bmwwindowgreen"
];

Just move the line: 只需移动线:

 <script type="text/javascript" language="javascript" src="../JS/bmwcustom.js"></script>

right before your </body> tag. 就在你的</body>标签之前。

First load your HTML elements, then Javascript and it should work. 首先加载你的HTML元素,然后加载Javascript,它应该工作。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Uncaught ReferenceError: (function) is not defined at HTMLButtonElement.onclick - Uncaught ReferenceError: (function) is not defined at HTMLButtonElement.onclick 未捕获的ReferenceError:在HTMLButtonElement.onclick中未定义函数 - Uncaught ReferenceError: function is not defined at HTMLButtonElement.onclick 得到此错误Uncaught ReferenceError:函数未在HTMLButtonElement.Onclick上定义? - Getting this error Uncaught ReferenceError: function is not defined at HTMLButtonElement.Onclick? ES6模块“Uncaught ReferenceError:函数未在HTMLButtonElement.onclick中定义” - ES6 module “Uncaught ReferenceError: function is not defined at HTMLButtonElement.onclick” 未捕获的ReferenceError:未在HTMLButtonElement.onclick中定义查找 - Uncaught ReferenceError: lookup is not defined at HTMLButtonElement.onclick 未捕获的 ReferenceError:showCurrentPage 未在 HTMLButtonElement.onclick 中定义 - Uncaught ReferenceError: showCurrentPage is not defined at HTMLButtonElement.onclick 未捕获的 ReferenceError:在 HTMLButtonElement.onclick 中未定义 postAcmgForm - Uncaught ReferenceError: postAcmgForm is not defined at HTMLButtonElement.onclick 未捕获的ReferenceError:HTMLButtonElement.onclick中未定义submitToAPI - Uncaught ReferenceError: submitToAPI is not defined at HTMLButtonElement.onclick 未捕获的 ReferenceError:id 未在 HTMLButtonElement.onclick 中定义 - Uncaught ReferenceError: id is not defined at HTMLButtonElement.onclick Angular 2 和 LeafLet 未捕获的 ReferenceError:<function> 未在 HTMLButtonElement.onclick 中定义 - Angular 2 and LeafLet Uncaught ReferenceError: <function> is not defined at HTMLButtonElement.onclick
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM