通过onClick属性将引用的父级ID传递给JS函数
我想从特定的父母div中获取子div的元素。我的DIV可能具有相同的ID,即id =“ PetatTribute
。我要做的是通过宠物详细信息列表迭代,将其信息存储在卡中,当我单击卡中的按钮时,它将提醒该特定卡的内容
。
function pet(){
let petAttribute = document.getElementById("petAttribute").children;
let petname = petAttribute[0].innerHTML;
alert(petname);
}
* {
box-sizing: border-box;
}
body {
font-family: Arial, Helvetica, sans-serif;
}
/* Float four columns side by side */
.column {
float: left;
width: 25%;
padding: 0 10px;
}
/* Remove extra left and right margins, due to padding */
.row {margin: 0 -5px;}
/* Clear floats after the columns */
.row:after {
content: "";
display: table;
clear: both;
}
/* Responsive columns */
@media screen and (max-width: 600px) {
.column {
width: 100%;
display: block;
margin-bottom: 20px;
}
}
/* Style the counter cards */
.card {
box-shadow: 0 4px 8px 0 rgba(0, 0, 0, 0.2);
padding: 16px;
text-align: center;
background-color: #f1f1f1;
}
img {
width: 100px;
height: 100px;
border-radius: 50%;
border: 1px solid black;
}
.button {
background-color: #4CAF50;
border: none;
color: white;
padding: 3px 3px;
text-align: center;
text-decoration: none;
display: inline-block;
font-size: 16px;
margin: 2px 2px;
cursor: pointer;
}
h2>Choose your pet to groom</h2>
<div class="row">
<div class="column">
<div class="card" id="petcard">
<img src="https://logowik.com/content/uploads/images/t_cat8600.jpg" alt="Paris" width="300" height="300">
<div id = "petAttribute">
<div id="name">Nomi</div>
<div id="breed">Domestic Long Hair</div>
<div id="age">Adult</div>
</div>
<input type="button" class="button" value="Groom my pet" onclick="pet()">
</div>
</div>
<div class="row">
<div class="column">
<div class="card" id="petcard">
<img src="https://logowik.com/content/uploads/images/t_cat8600.jpg" alt="Paris" width="300" height="300">
<div id="petAttribute">
<div id="name">kuchi</div>
<div id="breed">Domestic short Hair</div>
<div id="age">kitten</div>
</div>
<input type="button" class="button" value="Groom my pet" onclick="pet()">
</div>
</div>
i want to get the element of a child div from a SPECIFIC parent div. my div may have the same id which is id="petAttribute
. what im going to do is iterate through a list of pet details, store their information in a card and when i click the button IN the card, it will alert the content of that particular card.
the problem in my code was, i successfully prompt the name of the pet, but when i click on a different card, it does not prompt the name from the card that i clicked.
function pet(){
let petAttribute = document.getElementById("petAttribute").children;
let petname = petAttribute[0].innerHTML;
alert(petname);
}
* {
box-sizing: border-box;
}
body {
font-family: Arial, Helvetica, sans-serif;
}
/* Float four columns side by side */
.column {
float: left;
width: 25%;
padding: 0 10px;
}
/* Remove extra left and right margins, due to padding */
.row {margin: 0 -5px;}
/* Clear floats after the columns */
.row:after {
content: "";
display: table;
clear: both;
}
/* Responsive columns */
@media screen and (max-width: 600px) {
.column {
width: 100%;
display: block;
margin-bottom: 20px;
}
}
/* Style the counter cards */
.card {
box-shadow: 0 4px 8px 0 rgba(0, 0, 0, 0.2);
padding: 16px;
text-align: center;
background-color: #f1f1f1;
}
img {
width: 100px;
height: 100px;
border-radius: 50%;
border: 1px solid black;
}
.button {
background-color: #4CAF50;
border: none;
color: white;
padding: 3px 3px;
text-align: center;
text-decoration: none;
display: inline-block;
font-size: 16px;
margin: 2px 2px;
cursor: pointer;
}
h2>Choose your pet to groom</h2>
<div class="row">
<div class="column">
<div class="card" id="petcard">
<img src="https://logowik.com/content/uploads/images/t_cat8600.jpg" alt="Paris" width="300" height="300">
<div id = "petAttribute">
<div id="name">Nomi</div>
<div id="breed">Domestic Long Hair</div>
<div id="age">Adult</div>
</div>
<input type="button" class="button" value="Groom my pet" onclick="pet()">
</div>
</div>
<div class="row">
<div class="column">
<div class="card" id="petcard">
<img src="https://logowik.com/content/uploads/images/t_cat8600.jpg" alt="Paris" width="300" height="300">
<div id="petAttribute">
<div id="name">kuchi</div>
<div id="breed">Domestic short Hair</div>
<div id="age">kitten</div>
</div>
<input type="button" class="button" value="Groom my pet" onclick="pet()">
</div>
</div>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
首先,ID应该是唯一的。
getElementById
方法始终返回带有所需ID的第一个元素。 (这就是为什么您始终获得名字,无论您单击哪个按钮。)因此,您最好使用类:其次,如果您不将任何信息传递给应该打印名称的函数,则需要单击哪个按钮的最少信息。然后,您可以直接从此按钮导航到包含名称的元素。一种方法是以下方法:
First of all, IDs should be unique. The
getElementById
method always returns the first element with the desired ID. (That's why you always get the first name, no matter which button you click.) So you'd better use classes:Second, if you don't pass any information to the function that is supposed to print the name, you need at least information about which button was clicked. Then you can navigate from this button directly to the element that contains the name. One way to do this is the following: