简体   繁体   English

JS + JS =破解代码?

[英]JS + JS = broken code?

When I add 2 different javascript codes to the same document, one of them stops working properly; 当我在同一文档中添加2个不同的javascript代码时,其中一个停止正常工作; my viewport is meant to follow once the next question is clicked. 单击下一个问题后,便会遵循我的视口。 at the moment it doesn't, and I have no idea why. 目前还没有,我也不知道为什么。 I have checked variables names etc, and there are no duplicates. 我检查了变量名称等,并且没有重复项。

This has happened in the past, and I lost some functionality, I would really like this explained. 这是过去发生的事情,我失去了一些功能,我真的很想对此进行解释。

http://codepen.io/erayner/pen/mRrMVd (this is my final code) http://codepen.io/erayner/pen/VPvLyR (this is what it should be doing) http://codepen.io/erayner/pen/mRrMVd (这是我的最终代码) http://codepen.io/erayner/pen/VPvLyR (这是应该做的)

1st code 第一个代码

//when you click on an answer, the next answer appears

$(() => {
    init();
});

function init() {
    numberSections();
    bindAnswerClick();
    showSection(0);
}

function numberSections() {
    $(".section").each((index, elem) => {
        $(elem).data("index", index);
        $(elem).attr("data-index", index);
    });
}

function bindAnswerClick() {
    $(".answer").on("click", (e) => {
        selectAnswer($(e.currentTarget));
    });
}

function selectAnswer($answer) {
    let $section = $answer.closest(".section");
    let nextIndex = parseInt($section.data("index")) + 1;

    $section.find(".answer").removeClass("highlight");
    $answer.addClass("highlight");

    showSection(nextIndex);
}

function showSection(index) {
    $(".section[data-index='" + index + "']").show();
}

2nd code 第二码

//variables for end answer

var finalAnswers = [];
var button = document.getElementById("button");
//add answer values together and find end URL for button

$(document).ready(function () {

    $("a[data-value]").on("click", function (e) {
        var value = $(this).attr('data-value');
        finalAnswers.push(value);
        e.preventDefault();
    });

    $(".Finalbutton").click(function () {
        var store = finalAnswers;
        var frequency = {}; // array of frequency.
        var max = 0; // holds the max frequency.
        var result; // holds the max frequency element.
        for (var v in store) {
            frequency[store[v]] = (frequency[store[v]] || 0) + 1; // increment  frequency.
            if (frequency[store[v]] > max) { // is this frequency > max so far ?
            max = frequency[store[v]]; // update max.
            result = store[v]; // update result.
            }
        }

    if (result == "A")
        button.setAttribute("href", "http://www.w3schools.com");
    if (result == "B")
        button.setAttribute("href", "http://dailydropcap.com/images/C-9.jpg");
    if (result == "C")
        button.setAttribute("href", "http://www.w3schools.com");
    });

});

The reason is the following: 原因如下:

When ".answer" is clicked there is executed this code 单击“ .answer”时,将执行此代码

$(".answer").on("click", (e) => {
    selectAnswer($(e.currentTarget));
});

But when " a[data-value] " is clicked, then " .answer " is clicked too. 但是,当单击“ a[data-value] ”时,也将单击“ .answer ”。 Because of 因为

$(".answer").on("click", (e) => {
    selectAnswer($(e.currentTarget));
});

is earlier than 早于

$("a[data-value]").on("click", function (e) {
    var value = $(this).attr('data-value');
    finalAnswers.push(value);
    e.preventDefault();
});

so first code is executed, but second not. 因此执行第一个代码,但不执行第二个代码。

You should not use .on('click',... syntax. Much better is addEventListener . 你不应该使用.on('click',...语法。好多是addEventListener

Let add one .addEventListener on possible huge area when actions should be done after click. 单击后应执行操作时,请在可能的.addEventListener区域上添加一个.addEventListener Syntax is similar to the following: 语法类似于以下内容:

area.addEventListener('click',function(e) {
    if( e.taget ... ){ action 1 } else if( e.target ... ) { action 2 }and so on.
}

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM