【问题标题】:alphabetically sort an array of inputted words with javascript/jquery使用 javascript/jquery 按字母顺序对输入的单词数组进行排序
【发布时间】:2017-09-30 01:26:09
【问题描述】:

我是 javascript 和 jquery 的新手。我需要帮助找出我的代码出了什么问题,因为它在最后的步骤中没有执行“.sort()”和“.join(”、“)...

我希望页面有一个弹出框,用户在其中输入位置(输入存储在数组中)并继续,直到他们输入“完成”,然后页面按字母顺序加载他们的位置列表用逗号排序。我的第一部分没问题(提示和输入文本),但我的页面在输入“完成”后没有加载输入的材料。以下是我的代码:

        <div id="outputPlaces"></div>

        <script>
            $(document).ready(function() {
                var favPlaces = [];
                var input = prompt("Please enter your favorite place or type done to stop entering places.");
                while (input != 'done') {
                    favPlaces.push(input);
                    input = prompt("Please enter another favorite place or type done to stop entering places.");
                }
                favPlaces.sort();
                $('#outputPlaces').html = favPlaces.join(", ")

            });

        </script>

【问题讨论】:

  • $().html 是一个函数,您需要像这样调用:$('#outputPlaces').html(favPlaces.join(", "))
  • @Xufox 哎呀,要输入 favPlaces,我现在修好了..

标签: javascript jquery arrays sorting alphabetical


【解决方案1】:

好像你有favWords 而不是favPlaces

.html(favPlaces.join(", "))

【讨论】:

  • 这是错误的:html 是一个带有可选参数的函数。如果它丢失,它获取内部 HTML,否则它设置它。问题不在于texthtml,而在于如何使用该功能。
  • 你是对的,这是错误的,这就是为什么我不应该使用 SO 移动应用程序......我正在更新我的答案。
【解决方案2】:

因为 favWords 没有定义。也许您的意思是 favPlaces.join(", ") 并尝试将 $('#outputPlaces').html = favPlaces.join(", ") 更改为 document.getElementById("outputPlaces").innerHTML = favPlaces.join(", ")

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-02-08
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多