英文:
How can I get element values inside an element selected by ID?
问题
我阅读了使用以下代码在元素上的代码:
const elem = document.getElementById("TcHmiRecipeEdit");
console.log(elem);
在JavaScript中,我需要使用什么代码来获取照片中标记的值?我开始学习JavaScript,我们实际上遇到的一个巨大问题是从上面的示例代码中提取值。
英文:
I read code on element using
const elem = document.getElementById("TcHmiRecipeEdit")
console.log(elem);
results:
What code in Javascript I need to use to get values Marked in the photo. I'm starting my adventure with javascript and what we have a huge problem with is actually extracting values from the code as in the example above.
答案1
得分: 2
querySelector
和 querySelectorAll
适用于此目的。
querySelector,querySelectorAll
document.querySelectorAll('#TcHmiRecipeEdit table th').forEach(el => {
console.log(el.textContent);
});
英文:
querySelector
and querySeletorAll
are suitable for this purpose.
querySelector, querySelectorAll
document.querySelectorAll('#TcHmiRecipeEdit table th').forEach(el => {
console.log(el.textContent);
});
答案2
得分: 1
以下是翻译好的部分:
const thVals = [...document.querySelectorAll('#TchmiRecipeEdit table th')]
.map(th => th.textContent.trim());
console.log(thVals)
<div id="TchmiRecipeEdit" style="inset: 16px;">
<div class="TCHES Controls Beckhoff TcHiRecipeEdit template tched-box" tabindex="-1">
<div class="Tched Controls Beckhoff TcHnRecipeEdit-editing pane">
<div class="TcHed_Controls Beckhoff TcHndRecipeEdit-editing-box">
<div class="Tchad Controls Beckhoff TcHniRecipeEdit-editing-area">
<table>
<thead>
<tr>
<th>Name</th>
<th>Value</th>
<th>in</th>
<th>Unit</th>
</tr>
</thead>
</table>
</div>
</div>
</div>
</div>
</div>
英文:
Spreading a querySelectorAll and mapping the textContent
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
const thVals = [...document.querySelectorAll('#TchmiRecipeEdit table th')]
.map(th => th.textContent.trim());
console.log(thVals)
<!-- language: lang-html -->
<div id="TchmiRecipeEdit" style="inset: 16px;">
<div class=" TCHES Controls Beckhoff TcHiRecipeEdit template tched-box " tabindex="-1 ">
<div class="Tched Controls Beckhoff TcHnRecipeEdit-editing pane ">
<div class="TcHed_Controls Beckhoff TcHndRecipeEdit-editing-box">
<div class="Tchad Controls Beckhoff TcHniRecipeEdit-editing-area">
<table>
<thead>
<tr>
<th>Name</th>
<th>Value</th>
<th>in</th>
<th>Unit</th>
</tr>
</thead>
</table>
</div>
</div>
</div>
</div>
</div>
<!-- end snippet -->
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论