[英]Javascript - Retrieving values of a div when a button inside that div is clicked
I'm writing the code to edit a database table.我正在编写代码来编辑数据库表。
I have the following HTML:我有以下 HTML:
<div id="1">
<div contenteditable>aaa</div>
<div contenteditable>bbb</div>
<div contenteditable>ccc</div>
<button onClick="a('save')">SAVE</button>
<button onClick="a('delete')">DELETE</button>
</div>
<div id="2">
<div contenteditable>ddd</div>
<div contenteditable>eee</div>
<div contenteditable>fff</div>
<button onClick="a('save')">SAVE</button>
<button onClick="a('delete')">DELETE</button>
</div>
<div id="3">
<div contenteditable>ggg</div>
<div contenteditable>hhh</div>
<div contenteditable>iii</div>
<button onClick="a('save')">SAVE</button>
<button onClick="a('delete')">DELETE</button>
</div>
And so on.等等。
Using the following function, I can get the clicked button:使用以下 function,我可以获得单击的按钮:
function a(value) {
console.log(value);
}
When a button (SAVE or DELETE) is clicked, I need to retrieve:单击按钮(保存或删除)时,我需要检索:
Is it possible using pure Javascript?是否可以使用纯 Javascript?
Any suggestion will be very appreciated.任何建议将不胜感激。
Thanks in advance.提前致谢。
What I would do is implement click listeners in JS, that way I can query elements easily.我要做的是在 JS 中实现点击监听器,这样我就可以轻松查询元素。
Here is the example:这是示例:
// Query all div.div-editable elements document.querySelectorAll('div.div-editable').forEach((div) => { // The id of the parent const divId = div.id; // Each of content editable divs inside the parent div const editables = div.querySelectorAll('div[contenteditable]'); // The buttons Save and Delete const saveBtn = div.querySelector('button.button-save'); const deleteBtn = div.querySelector('button.button-delete'); // Add click listeners to buttons saveBtn.addEventListener('click', function() { console.log('Saved: ' + divId); const contentOfEditableDivs = Array.from(editables).map((div) => div.innerText); console.log('Values of divs:', contentOfEditableDivs); }); deleteBtn.addEventListener('click', function() { console.log('Deleted: ' + divId); const contentOfEditableDivs = Array.from(editables).map((div) => div.innerText); console.log('Values of divs:', contentOfEditableDivs); }); });
<div id="1" class="div-editable"> <div contenteditable>aaa</div> <div contenteditable>bbb</div> <div contenteditable>ccc</div> <button class="button-save">SAVE</button> <button class="button-delete">DELETE</button> </div> <div id="2" class="div-editable"> <div contenteditable>ddd</div> <div contenteditable>eee</div> <div contenteditable>fff</div> <button class="button-save">SAVE</button> <button class="button-delete">DELETE</button> </div> <div id="3" class="div-editable"> <div contenteditable>ggg</div> <div contenteditable>hhh</div> <div contenteditable>iii</div> <button class="button-save">SAVE</button> <button class="button-delete">DELETE</button> </div>
EDIT 1: Added code snippet编辑 1:添加代码片段
EDIT 2: Simplified explanation编辑 2:简化说明
You can send this
keyword in the argument of click's event handler and then access the parent div's id.您可以在 click 的事件处理程序的参数中发送
this
关键字,然后访问父 div 的 id。
So your HTML would look something like:所以你的 HTML 看起来像:
// rest of the code here
<button onClick="a(this, 'save')">SAVE</button>
<button onClick="a(this, 'delete')">DELETE</button>
// rest of the code here
And your JS code would change to:您的 JS 代码将更改为:
function a(elem, value) {
console.log(elem.parentNode.id);
}
More details on the following link: how i get parent id by onclick Child in js有关以下链接的更多详细信息: how i get parent id by onclick Child in js
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.