這是我單擊按鈕時的 html 模板表單。
<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 >
<h3>Person2</h3>
<p>Phone: 0111111111</p>
<button onclick="addToHistory()">Call</button>
</div>`
現在,我想點擊一個按鈕,然后根據我的點擊,我想要存盤被點擊的 div 的資料。
正如您在 addToHistory() 函式中看到的那樣,我使用事件處理程式進行了嘗試。但是,它存盤了我單擊和未單擊的所有資料。
請注意:我只想使用 JavaScript 執行此操作。謝謝您的支持。
uj5u.com熱心網友回復:
您可以傳入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>`
uj5u.com熱心網友回復:
We can trigger button click by class.
Please check this example `https://jsfiddle.net/h54dt731/`
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/395440.html
標籤:javascript
上一篇:不透明畫布上的透明圓圈
