javascript - 函数在 JQuery 中给出未定义的错误

标签 javascript jquery html arrays slider

我正在尝试将 slider 集成到我的应用程序中。 我指的是这个w3schools sample 。 但由于某种原因,每当页面加载时,图像都会出现一秒钟然后消失,当我单击箭头或点时,我会收到如下所示的函数未定义错误。

Uncaught ReferenceError: plusSlides is not defined at HTMLAnchorElement.onclick (details.html:1)

任何帮助将不胜感激,提前致谢......!!! 请在下面找到我的代码。

<!DOCTYPE html>
<html>
    <head>
        <!-- Meta -->
        <meta charset="utf-8">
        <title>Xylem</title>
        <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <meta name="author" content="Samuel Norton">

        <!-- Styles -->
        <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
        <link rel="stylesheet" type="text/css" href="css/reset.css">
        <link rel="stylesheet" href="css/style.css">
        <link href='http://fonts.googleapis.com/css?family=Lato:300,400,700,900|Raleway:400,300,700,600,900' rel='stylesheet' type='text/css'>
        <script src="http://code.jquery.com/jquery-1.10.2.js"></script>
        <script src="http://code.jquery.com/ui/1.11.1/jquery-ui.js"></script>
        <script src="http://code.jquery.com/color/jquery.color-2.1.0.min.js"></script>
        <style>
                * {box-sizing:border-box}
                .mySlides {display:none}

                /* Slideshow container */
                .slideshow-container {
                max-width: 1000px;
                position: relative;
                margin: auto;
                }

                /* Next & previous buttons */
                .prev, .next {
                cursor: pointer;
                position: absolute;
                top: 50%;
                width: auto;
                padding: 16px;
                margin-top: -22px;
                color: white;
                font-weight: bold;
                font-size: 18px;
                transition: 0.6s ease;
                border-radius: 0 3px 3px 0;
                }

                /* Position the "next button" to the right */
                .next {
                right: 0;
                border-radius: 3px 0 0 3px;
                }

                /* On hover, add a black background color with a little bit see-through */
                .prev:hover, .next:hover {
                background-color: rgba(0,0,0,0.8);
                }

                /* Caption text */
                .text {
                color: #f2f2f2;
                font-size: 15px;
                padding: 8px 12px;
                position: absolute;
                bottom: 8px;
                width: 100%;
                text-align: center;
                }

                /* Number text (1/3 etc) */
                .numbertext {
                color: #f2f2f2;
                font-size: 12px;
                padding: 8px 12px;
                position: absolute;
                top: 0;
                }

                /* The dots/bullets/indicators */
                .dot {
                cursor: pointer;
                height: 15px;
                width: 15px;
                margin: 0 2px;
                background-color: #bbb;
                border-radius: 50%;
                display: inline-block;
                transition: background-color 0.6s ease;
                }

                .active, .dot:hover {
                background-color: #717171;
                }

                /* Fading animation */
                .fade {
                -webkit-animation-name: fade;
                -webkit-animation-duration: 1.5s;
                animation-name: fade;
                animation-duration: 1.5s;
                }

                @-webkit-keyframes fade {
                from {opacity: .4} 
                to {opacity: 1}
                }

                @keyframes fade {
                from {opacity: .4} 
                to {opacity: 1}
                }

                /* On smaller screens, decrease text size */
                @media only screen and (max-width: 300px) {
                .prev, .next,.text {font-size: 11px}
                }
            </style>
    </head>
    <body class="bgColor">
        <div class="container-fluid">
            <div>
                <div>
                    <div>
                        <div class="productContent">
                            <a class="nextlinkProcuct"> &#8592 GO BACK</a>
                            <div class="row containerDiv">
                                <div class="col-md-12 pd10-top pd10-bottom">
                                    <div class="slideshow-container">

                                        <!-- Next and previous buttons -->

                                    </div>
                                    <br>

                                    <div style="text-align:center">
                                    <span class="dot" onclick="currentSlide(1)"></span> 
                                    <span class="dot" onclick="currentSlide(2)"></span> 
                                    <span class="dot" onclick="currentSlide(3)"></span>
                                    <span class="dot" onclick="currentSlide(4)"></span> 
                                    <span class="dot" onclick="currentSlide(5)"></span> 
                                    </div>
                                </div>
                                <div class="col-md-12 title">360 View</div>
                                <div style="width: 400px;" class="col-md-4">
                                        <script src='https://vizor.io/static/scripts/vizor-360-embed.js' data-vizorurl='https://vizor.io/embed/anamikabadal/americanorthcarolinamorrisville'></script>
                                        <a href="#" data-rel="back"></a>
                                </div>
                            </div>

                        </div>
                    </div>
                </div>
            </div>
        </div>
        <script>
                $( document ).ready(function() {
                    var i;
                    var detailsDataObj = {
                        "Title":"XYLEM WATER SOLUTIONS INDIA PVT LTD.",
                        "Description":"This page will aim to list all the high level modules and the functionality (use cases) for each module. Each module can be considered an EPIC and sub-bulleted use case as super-stories. Each Super-story will have stories that will be tracked in Jira and used for Sprint planning. This page will only list epics and super - stories.",
                        "sliderImage":["img1.jpg","img2.jpg","img3.jpg","img4.jpg","img5.jpg"],
                        "Image360":"images/testing.jpeg"
                    }   
                    var titleDiv = '<div class="title col-md-12 pd10-top pd10-bottom centerText">'+ detailsDataObj.Title + '</div>';              // Create text with HTML
                    $(".containerDiv").append(titleDiv);     // Append new elements
                    var pageDesc = '<div class="discription col-md-12 pd10-top pd10-bottom">'+ detailsDataObj.Description + '</div>';
                    $(".containerDiv").append(pageDesc);     // Append new elements
                    var prevArrow = '<a class="prev" onclick="plusSlides(-1)">&#10094;</a>';
                    var nextArrow = '<a class="next" onclick="plusSlides(1)">&#10095;</a>';
                    for(i=0;i<detailsDataObj.sliderImage.length;i++){
                        var slideContainer = '<div class="mySlides fade">';
                        var img = '<img height="300" ';
                        img+='src="'+detailsDataObj.sliderImage[i]+'"';
                        img+='/>';
                        slideContainer+=img;
                        slideContainer+='</div>';
                        $('.slideshow-container').append(slideContainer);
                    }
                    $('.slideshow-container').append(prevArrow,nextArrow);

                    var slideIndex = 1;
                    showSlides(slideIndex);

                    function plusSlides(n) {
                    showSlides(slideIndex += n);
                    }

                    function currentSlide(n) {
                    showSlides(slideIndex = n);
                    }

                    function showSlides(n) {
                    var i;
                    var slides = document.getElementsByClassName("mySlides");
                    var dots = document.getElementsByClassName("dot");
                    if (n > slides.length) {slideIndex = 1}    
                    if (n < 1) {slideIndex = slides.length}
                    for (i = 0; i < slides.length; i++) {
                        slides[i].style.display = "none";  
                    }
                    for (i = 0; i < dots.length; i++) {
                        dots[i].className = dots[i].className.replace(" active", "");
                    }
                    slides[slideIndex-1].style.display = "block";  
                    dots[slideIndex-1].className += " active";
                    }

                });
                </script>
    </body>
</html>

最佳答案

您正尝试从 HTML 文档全局调用函数:

onclick="plusSlides(-1)"

但是这些函数是在另一个函数内的封闭作用域中定义的:

$(document).ready(function() {

    //...

    function plusSlides(n) {
        showSlides(slideIndex += n);
    }

    //...

});

传递给 document.ready 处理程序的匿名 function() {} 内部没有任何定义,外部将可见 em>那个函数。

您可以将函数分配给 window 对象:

$(document).ready(function() {

    //...

    window.plusSlides = function (n) {
        showSlides(slideIndex += n);
    };

    //...

});

或者定义该范围之外的函数:

$(document).ready(function() {

    //...

});

function plusSlides(n) {
    showSlides(slideIndex += n);
}

//...

出于同样的原因,您的其他函数、对这些函数的引用等也可能需要调整。函数外部的内容可以从函数内部访问,但反之则不行。

关于javascript - 函数在 JQuery 中给出未定义的错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47800627/

相关文章:

jquery - 在发出 AJAX 请求时使用 jQuery 显示加载栏

javascript - 如何使输入字段末尾的 fontAwesome 图标也可点击

javascript - 由模态按钮操作设置的元素值在 jquery 中不起作用?

javascript - 将所有纯文本替换为同一 div 的类

javascript - $.fn.submit() 事件不会在 &lt;iframe&gt; 中触发

javascript - 简单的 javascript 验证会阻止用户在输入无效日期格式时提交表单?

javascript - 有没有一种方法可以使用类似于 contenteditable 的文本区域的用户提交值来更新 DOM?

javascript - 如何在基于 Web 的应用程序中提供剪切、复制、粘贴、撤消、重做、删除按钮的功能

javascript - 将相同的多个对象推送到多个数组中

javascript - 为 PhoneGap Android 应用程序使用 Javascript 的简单图片库