【问题标题】:How To Convert Value To Array如何将值转换为数组
【发布时间】:2020-08-21 12:54:40
【问题描述】:

我有以下代码部分,我的朋友为一个项目编写了代码,它使用了 jQuery 库。我不知道如何把它放在 Javascript 中,这样我就可以从项目中删除 jQuery。

function generateButtonClicked() {
    disableGenerateButton();
    var t = $(".speed").toArray(),
        e = $(".status").toArray();
    t.reverse(), e.reverse(), doNextBox(t, e)
}

function tripConnecting(t, e, n, a, c, l) {
    $(c).html("Connecting"), tripDestination(n, function () {
        tripMeasurement(t, e, n, a, c, l)
    })
}

我使用online tool 来转换它,它给了我以下代码,但它不能正常工作。任何帮助将不胜感激。

functiongenerateButtonClicked() {
    disableGenerateButton().vart = (".speed").toArray(), e = (".status").toArray().t.reverse(), e.reverse(), doNextBox(t, e)
}
functiontripConnecting(t, e, n, a, c, l) {
    (c).html("Connecting"), tripDestination(n, function () {
        tripMeasurement(t, e, n, a, c, l)
    })
}

【问题讨论】:

    标签: javascript jquery


    【解决方案1】:

    试试这个:

    function generateButtonClicked() {
        disableGenerateButton();
        var t = Array.from(document.getElementsByClassName("speed")),
            e = Array.from(document.getElementsByClassName("status"));
        t.reverse(); // You could also add `.reverse` to the above declarations.
        e.reverse();
        doNextBox(t, e);
    }
    
    function tripConnecting(t, e, n, a, c, l) {
        c.innerHTML = "Connecting";
        tripDestination(n, function () {
            tripMeasurement(t, e, n, a, c, l);
        });
    }
    

    【讨论】:

    • 完美运行。感谢您抽出宝贵时间对此进行调查。
    • 哦,等等!我错误地将; 而不是, 放在第三行。我要编辑我的答案。
    【解决方案2】:

    jQuery 中的 toArray 方法如下所示:

    const elements = Array.from(document.querySelectorAll('.speed'))
    

    【讨论】:

    • $(c).html("Connecting") 可以替换为 el.textContent = "Connecting" 如果它不是一个集合。如果只是迭代它们
    猜你喜欢
    • 2016-01-13
    • 1970-01-01
    • 1970-01-01
    • 2011-08-12
    • 2021-11-01
    • 2018-06-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多