Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/373.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/74.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
将JavaScript列表添加到网页_Javascript_Html_Dom - Fatal编程技术网

将JavaScript列表添加到网页

将JavaScript列表添加到网页,javascript,html,dom,Javascript,Html,Dom,在课堂上,我们的任务是通过将对象放入数组来创建水果及其价格的杂货清单。既然我完成了这一部分,我应该从上一个实验室扩展购物车程序。设置一个基本的HTML页面 将购物清单中的物品及其价格附加到页面。” 这就是我迷路的地方。我们只能使用香草JavaScript。**我可以让网页显示“总计”按钮,它可以工作(它计算了项目),但我的食品清单和价格在哪里 <head> <script src="monday2assn2.js"></script> </head>

在课堂上,我们的任务是通过将对象放入数组来创建水果及其价格的杂货清单。既然我完成了这一部分,我应该从上一个实验室扩展购物车程序。设置一个基本的HTML页面 将购物清单中的物品及其价格附加到页面。” 这就是我迷路的地方。我们只能使用香草JavaScript。**我可以让网页显示“总计”按钮,它可以工作(它计算了项目),但我的食品清单和价格在哪里

<head>
<script src="monday2assn2.js"></script>
</head>
<body>

<h1> Shopping List </h1>

<p>Click "Total" to add up your shopping list.</p>


<button onclick="calcTotal()">Total</button>

<p id="total"> </p>
我的代码:

var fruita = {
name: 'apple',
price: 5
};

var fruitb = {
name: 'pear',
price:3
};

var fruitc = {
name: 'orange',
price: 4
};

var grocery = [];
grocery.push(fruita, fruitb, fruitc);


var total = (fruita.price + fruitb.price +fruitc.price);

for (i=0; i<grocery.length; i++){
console.log(grocery[i].name);
console.log(grocery[i].price);
}

var total = (fruita.price + fruitb.price +fruitc.price);
console.log("total price= " + total);

function calcTotal() {
document.getElementById("total").innerHTML = total;
}

function displayList() {
document.write(grocery).innerHTML = grocery;
<head>
<script src="monday2assn2.js"></script>
</head>
<body>

<h1> Shopping List </h1>

<p>Click "Total" to add up your shopping list.</p>


<button onclick="calcTotal()">Total</button>

<p id="total"> </p>
var-fructa={
名称:'苹果',
价格:5元
};
变量b={
名字:'梨',
价格:3
};
变量c={
名称:“橙色”,
价格:4
};
var=[];
推(水果A、水果B、水果C);
var总计=(水果A.price+水果B.price+水果C.price);

对于(i=0;i一个好的开始方法是创建一个全局函数,该函数标准化了向页面添加元素的方法。以下是可用于向父元素添加元素的函数:

<head>
<script src="monday2assn2.js"></script>
</head>
<body>

<h1> Shopping List </h1>

<p>Click "Total" to add up your shopping list.</p>


<button onclick="calcTotal()">Total</button>

<p id="total"> </p>
function addElement(type,content,parent) {
    var newElement = document.createElement(type);
    var newContent = document.createTextNode(content);
    newElement.appendChild(newContent);
    get(parent,'id');
    current.insertBefore(newElement,current.childNodes[0]);
}
function get(reference, type) {
    switch(type) {
        case 'id':
        current = document.getElementById(reference);
        break;
        case 'class':
        current = document.getElementsByClassName(reference);
        break;
        case 'tag':
        current = document.getElementsByTagName(reference);
        break;
    }
}
我从自己的文件中复制并粘贴了它,因为我经常使用这些文件。get函数用于选择元素。addElement函数用于创建新元素。“type”参数指定标记-so p、div、span等。“Content”和“parent”非常简单。“parent”参数参数由父项的id表示。完成后,您可以执行以下操作:

<head>
<script src="monday2assn2.js"></script>
</head>
<body>

<h1> Shopping List </h1>

<p>Click "Total" to add up your shopping list.</p>


<button onclick="calcTotal()">Total</button>

<p id="total"> </p>
for(var i=0;i<grocery.length;i++) {
    var concat = 'Item: ' + grocery[i].name + ' Price: ' + grocery[i].price;
    addElement('p',concat,'body'); //assign the body tag an id of "body"
}

for(var i=0;i阅读Document.createElement()和Node.appendChild()的文档,然后再试一次。如果您仍然需要帮助,请编辑您的问题以包含您的尝试。