gpt4 book ai didi

javascript - 我犯的错误是什么阻止我访问对象属性?

转载 作者:行者123 更新时间:2023-11-30 15:26:58 26 4
gpt4 key购买 nike

我遇到了一个我自己也想不通的问题。我的任务是:

  1. 创建 onload 事件处理程序 init()
  2. 在 init() 中,创建 3 个 javascript 对象,每个对象都具有相同的 6 个属性(相关的一个是价格)
  3. 初始化 3 个对象的属性值
  4. 创建一个包含 3 个按钮的 HTML 页面(每种类型的对象一个)。这些按钮每个都有一个 onclick 事件,它会激活一个名为“getPrice”的事件处理程序
  5. 为输出文本创建一个 div
  6. 在 JS 事件处理程序中,编写代码以便输出文本显示单击的按钮的名称和相应的价格

我的主要问题是我无法理解如何从对象中提取 Price 属性。这是我的 HTML:

<!DOCTYPE html>
<html>
<head>
<title></title>
<meta charset="utf-8" />
<script type="text/javascript" src="Scripts/Script.js"></script>
</head>
<body onload="init()">
<input id="btnKoalas" type="button" name="Koalas" value="Koalas" onclick="getPrice()"/>
<input id="btnTulips" type="button" name="Tulips" value="Tulips" onclick="getPrice()"/>
<input id="btnPenguins" type="button" name="Penguins" value="Penguins" onclick="getPrice()"/>

<br />

<p id="divText">This is the output text</p>

</body>
</html>

这是我的javascript

function init()
{

function Koala(ProdID, SupplierCode, Description, PictureName, QtyOnHand, Price) {
this.ProdID = ProdID;
this.SupplierCode = SupplierCode;
this.Description = Description;
this.PictureName = PictureName;
this.QtyOnHand = QtyOnHand;
this.Price = Price;
}

function Tulip(ProdID, SupplierCode, Description, PictureName, QtyOnHand, Price) {same as above}

function Penguin(ProdID, SupplierCode, Description, PictureName, QtyOnHand, Price) {same as above}

}


objKoala = new Koala('1002', 'S1001', 'Koalas', 'Koala.jpg', '9', '119.95');

objTulip = new Tulip('1003', 'S1002', 'Tulips', 'Tulip.jpg', '9', '7.95');

objPenguin = new Penguin('1004', 'S1003', 'Penguins', 'Penguin.jpg', '9', '127.95');


function getPrice()
{
var output = objKoala.Price;
//i know this ^ isn't right.. i'm floundering trying to directly pull the data from my object above and it's still not displaying it
document.getElementById('divText').innerHTML = output;
}

我以为我已经将我的对象设置为全局对象,但是当我单击我的按钮时,没有任何反应。我如何将参数传递给 getPrice() 函数以拉取属性?我创建对象的方式是否有误?

最佳答案

试试这个而不是那个 body 加载。我将事件监听器移到了 js 本身中。这也适用于所有动物。注意 price 函数中的 this.name 。这应该采用按钮的名称属性并访问它各自的属性,而无需将其作为参数发送。

var animals = {};

function init() {

// The event listeners for each button
document.getElementById('btnKoalas').addEventListener('click', getPrice);
document.getElementById('btnTulips').addEventListener('click', getPrice);
document.getElementById('btnPenguins').addEventListener('click', getPrice);

// We don't need to do it in here, we can also put it outside.
animals.Koala = new Koala('1002', 'S1001', 'Koalas', 'Koala.jpg', '9', '119.95');
animals.Tulip = new Tulip('1003', 'S1002', 'Tulips', 'Tulip.jpg', '9', '7.95');
animals.Penguin = new Penguin('1004', 'S1003', 'Penguins', 'Penguin.jpg', '9', '127.95');
}


function Koala(ProdID, SupplierCode, Description, PictureName, QtyOnHand, Price) {
this.ProdID = ProdID;
this.SupplierCode = SupplierCode;
this.Description = Description;
this.PictureName = PictureName;
this.QtyOnHand = QtyOnHand;
this.Price = Price;
}

function Tulip(ProdID, SupplierCode, Description, PictureName, QtyOnHand, Price) {same as above}

function Penguin(ProdID, SupplierCode, Description, PictureName, QtyOnHand, Price) {same as above}

// This gets called when the user clicks the button
function getPrice() {

// 'this' here is the element that was clicked, which can be any of the 3 buttons
// And we can dynamically access the animal based on the name attribute of the currently clicked button
var output = animals[this.name].Price;

document.getElementById('divText').innerHTML = output;
}

// We wait for the window to load, inline event listeners like '<body onload="">' are a bad idea
window.addEventListener('DOMContentLoaded', init);

和 html

<!DOCTYPE html>
<html>
<head>
<title></title>
<meta charset="utf-8" />
<script type="text/javascript" src="Scripts/Script.js"></script>
</head>
<body>
<input id="btnKoalas" type="button" name="Koala" value="Koalas"/>
<input id="btnTulips" type="button" name="Tulip" value="Tulips"/>
<input id="btnPenguins" type="button" name="Penguin" value="Penguins"/>

<br />

<p id="divText">This is the output text</p>

</body>
</html>

关于javascript - 我犯的错误是什么阻止我访问对象属性?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42816104/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com