DOM

时间:2016-04-20 22:41:02

标签: javascript html ajax

我遇到的问题是,从下拉菜单中选择项目后,我正在进行的AJAX请求会闪烁。从文本的闪存看起来,请求正在提取正确的信息。

它应该工作的方式是将鼠标悬停在下拉列表上,当点击锚标记时显示该类型的电影。

我认为发生的事情是,在进行另一次选择之前,我不会保持这种类型,如果在选择新类型之前如何保持类型值?如果不是我做错了吗?

我删除了所有的' fluff'从代码,所以我们只是有骨头。

HTML:

<!DOCTYPE html>
<html lang="en">
<head>
    <script src="script.js"></script>
    <style>
        .dropdown{
            width: 151px;
        }
        .dropbtn{
            width: 150px;
            height: 30px;
            border: 1px solid black;
            border-radius: 2px;
            background: white;
        }
        a{
            display: block;
            background-color: lightgreen;
            width: 148px;
            height: 30px;
            text-align: center;
            text-decoration: none;
            padding-top: 10px;
            border-bottom: 1px solid black;
            border-right: 1px solid black;
            border-left: 1px solid black;
        }
        a:hover{
            background-color: lightblue;
        }
        .content{
            display: none;
        }
        .dropdown:hover .content{
            display: block;
        }

    </style>
</head>
<body onload='movieScript()'>   

    <div class="dropdown">
        <button class="dropbtn">Select</button>
        <div class="content">
            <a class="anchor" href="" onclick="generateMovies('Comedy')">Comedy</a>
            <a class="anchor" href="" onclick="generateMovies('Action')">Action</a>
            <a class="anchor" href="" onclick="generateMovies('Biography')">Biography</a>
            <a class="anchor" href="" onclick="generateMovies('Drama')">Drama</a>
            <a class="anchor" href="" onclick="generateMovies('Crime')">Crime</a>
        </div>
    </div>

    <div id="selectedMovies"></div>

</body> 
</html>

JS:

var titles = [];//Will hold an array of titles
var imgs = [];//Will hold an array of imgs
var pos = 0;//Global position indicator which will be shared by all global arrays
movieScript = function(){
    var movieData = "http://test.frontendhero.nl/movie-challenge/movies.json";
    var myRequest;
    //Test if XMLHttpRequest is available in active browser
    try{
        myRequest = new XMLHttpRequest();
    }
    catch(e){
        try{
            myRequest = new ActiveXObject("Msxml2.XMLHTTP");
        }catch(e){
            try{
                myRequest = new ActiveXObject("Microsoft.XMLHTTP");
            }
            catch(e){
                alert("Something broke!");
                return false;
            }
        }
    }
    myRequest.onreadystatechange = function(){
        if(myRequest.readyState === 4 && myRequest.status === 200){              
            var jsonObj = JSON.parse(myRequest.responseText);//Grabs json object
            var data = jsonObj.data; //Grabbing data objects from json
            var assets; //Will hold array of data objects
            var movies =[]; //Array for actionMovie objects
            var DOM = document.getElementById('selectedMovies');

            generateMovies = function(userGenre){
                //Outer level of json
                for(var key in data){
                    assets = data[key].assets;

                    //Movie data of the json file cross checked with user selected genre
                    for(var x in assets){
                        if(assets[x].genre === userGenre){
                            movies[x] = assets[x];
                        }
                    }
                }
                //Add each element to an array of titles and imgms
                for(var key in movies){                                   
                    DOM.innerHTML = movies[key].title + movies[key].img;
                }
            }
        }
    }
    myRequest.open("GET", movieData, true);
    myRequest.send();
};

感谢任何帮助。

1 个答案:

答案 0 :(得分:1)

当您点击该链接时,它会链接到href网址,空网址表示重新加载当前网页。您可以通过从false

返回onclick来阻止此操作
<a class="anchor" href="" onclick="generateMovies('Comedy'); return false;">Comedy</a>

或者您可以使用javascript:void(0)网址:

<a class="anchor" href="javascript:void(0)" onclick="generateMovies('Comedy')">Comedy</a>

或者您可以使用发现的#网址。