亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

嘗試從 JavaScript 在 HTML 頁面上顯示段落

嘗試從 JavaScript 在 HTML 頁面上顯示段落

慕田峪7331174 2022-08-04 10:36:28
我的 JavaScript 有問題。我正在嘗試顯示向用戶顯示移動電話的用戶首選項的操作的結果。當我在Web瀏覽器中測試此代碼時,我看到一個空白的網頁,JavaScript控制臺中沒有錯誤。我在這里做錯了什么嗎?下面顯示的是我的代碼。// This is an array of the objects in the website, showing the phone information that will be used to filter for the user to view. const phones = [{  name: "iPhone XS", brand: "Apple", cost: 43, data: "500MB", minutes: "Unlimited", texts: "Unlimited"},{  name: "iPhone 11", brand: "Apple", cost: 64, data: "90GB", minutes: "Unlimited", texts: "Unlimited"},{  name: "Galaxy S10", brand: "Samsung", cost: 30, data: "20GB", minutes: "Unlimited", texts: "Unlimited"},{  name: "Galaxy S10", brand: "Samsung", cost: 65, data: "Unlimited", minutes: "Unlimited", texts: "Unlimited"},{  name: "Galaxy A10", brand: "Samsung", cost: 11.99, data: "500MB", minutes: 250, texts: "Unlimited"},{  name: "Galaxy S9", brand: "Samsung", cost: 31, data: "20GB", minutes: "Unlimited", texts: "Unlimited"},{  name: "StarTAC 130", brand: "Motorola", cost: 3, data: "0MB", minutes: 200, texts: 500},{  name: "Pixel 3A", brand: "Google", cost: 23, data: "4GB", minutes: "Unlimited", texts: "Unlimited"},{  name: "Xperia 10", brand: "Sony", cost: 30, data: "20GB", minutes: "Unlimited", texts: "Unlimited"},{  name: "P30", brand: "Huawei", cost: 27.99, data: "500MB", minutes: 500, texts: "Unlimited"}]// These are asking the user for entry of the data into the system.const phoneBrand = prompt("Enter a phone brand")const phoneCost = prompt("Enter a monthly cost")const phoneData = prompt("Enter the amount of data")const phoneMins = prompt("How many minutes?")const phoneTexts = prompt("How many texts?")// This is then filtering the object of phones to match what the user has entered into the system.const matchingPhones = phones.filter(function(phone) {    if(phone.brand===phoneBrand && phone.cost.toString()===phoneCost && phone.data===phoneData && phone.minutes.toString()===phoneMins && phone.texts.toString()===phoneTexts) {        return true;    }    return false;
查看完整描述

1 回答

?
肥皂起泡泡

TA貢獻1829條經驗 獲得超6個贊

我確實得到了這個回應,#1確保在定義Javascript之前定義returnPhones div:


<div id="returnPhones">


</div>

我已經測試了下面修改后的代碼,它的工作原理:


    const phones = [{

    name: "iPhone XS", brand: "Apple", cost: 43, data: "500MB", minutes: "Unlimited", texts: "Unlimited"

},

    {

        name: "iPhone 11", brand: "Apple", cost: 64, data: "90GB", minutes: "Unlimited", texts: "Unlimited"

    },

    {

        name: "Galaxy S10", brand: "Samsung", cost: 30, data: "20GB", minutes: "Unlimited", texts: "Unlimited"

    },

    {

        name: "Galaxy S10", brand: "Samsung", cost: 65, data: "Unlimited", minutes: "Unlimited", texts: "Unlimited"

    },

    {

        name: "Galaxy A10", brand: "Samsung", cost: 11.99, data: "500MB", minutes: 250, texts: "Unlimited"

    },

    {

        name: "Galaxy S9", brand: "Samsung", cost: 31, data: "20GB", minutes: "Unlimited", texts: "Unlimited"

    },

    {

        name: "StarTAC 130", brand: "Motorola", cost: 3, data: "0MB", minutes: 200, texts: 500

    },

    {

        name: "Pixel 3A", brand: "Google", cost: 23, data: "4GB", minutes: "Unlimited", texts: "Unlimited"

    },

    {

        name: "Xperia 10", brand: "Sony", cost: 30, data: "20GB", minutes: "Unlimited", texts: "Unlimited"

    },

    {

        name: "P30", brand: "Huawei", cost: 27.99, data: "500MB", minutes: 500, texts: "Unlimited"

    }]


// These are asking the user for entry of the data into the system.

const phoneBrand = prompt("Enter a phone brand")


const phoneCost = prompt("Enter a monthly cost")


const phoneData = prompt("Enter the amount of data")


const phoneMins = prompt("How many minutes?")


const phoneTexts = prompt("How many texts?")


// This is then filtering the object of phones to match what the user has entered into the system.


const matchingPhones = phones.filter(function(phone) {

    if(phone.brand===phoneBrand && phone.cost.toString()===phoneCost && phone.data===phoneData && phone.minutes.toString()===phoneMins && phone.texts.toString()===phoneTexts) {

        return true;

    }

    return false;


})


// This is then displaying data in the system.


const returnPhones = document.querySelector("#returnPhones");


matchingPhones.forEach(function(phone) {


    const newParagraph = document.createElement("p");

    newParagraph.textContent='The matching plans are as follows - ' + phone.name;

    returnPhones.appendChild(newParagraph);


})


查看完整回答
反對 回復 2022-08-04
  • 1 回答
  • 0 關注
  • 129 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號