![](/img/trans.png)
[英]I do not want all the "addToBasket" buttons to change when i click on a specefic "addToBasket" button. Only the clicked button should change
[英]I have a dynamic html template. Which forms when I click on a button. Now, the problem is When I click on one button all buttons are clicked
这是我单击按钮时的 html 模板 forms。
<div class="detail">
<h3>${element.name}</h3>
<p>Phone: ${element.phone}</p>
<button onclick="addToHistory()">Call</button>
</div>`
假设它按原样创建了两个模板。
<div class="detail">
<h3>Person1</h3>
<p>Phone: 0111111111</p>
<button onclick="addToHistory()">Call</button>
</div>`
<div class="detail">
<h3>Person2</h3>
<p>Phone: 0111111111</p>
<button onclick="addToHistory()">Call</button>
</div>`
现在,我想单击一个按钮,然后根据我的单击,我希望存储单击的 div 的数据。
正如您在 addToHistory() function 中看到的那样,我使用事件处理程序进行了尝试。 但是,它存储了我点击和未点击的所有数据。
请注意:我只想使用 JavaScript 来执行此操作。 谢谢您的支持。
您可以将this
传递到您的方法中,并使用它在单击按钮的特定detail
容器中进行遍历。
this
将引用事件发生的特定元素
function addToHistory(el){ console.log(el.previousElementSibling.textContent); }
<div class="detail"> <h3>Person1</h3> <p>Phone: 0999999999</p> <button onclick="addToHistory(this)">Call</button> </div>` <div class="detail"> <h3>Person2</h3> <p>Phone: 0111111111</p> <button onclick="addToHistory(this)">Call</button> </div>`
We can trigger button click by class.
Please check this example `https://jsfiddle.net/h54dt731/`
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.