如何获取通过ID选择的元素内的元素值?

huangapple go评论68阅读模式
英文:

How can I get element values inside an element selected by ID?

问题

我阅读了使用以下代码在元素上的代码:

const elem = document.getElementById("TcHmiRecipeEdit");
console.log(elem);

结果:
如何获取通过ID选择的元素内的元素值?

在JavaScript中,我需要使用什么代码来获取照片中标记的值?我开始学习JavaScript,我们实际上遇到的一个巨大问题是从上面的示例代码中提取值。

英文:

I read code on element using

const elem = document.getElementById("TcHmiRecipeEdit")
console.log(elem);

results:
如何获取通过ID选择的元素内的元素值?
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

querySelectorquerySelectorAll 适用于此目的。

querySelectorquerySelectorAll

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(&#39;#TchmiRecipeEdit table th&#39;)]
  .map(th =&gt; th.textContent.trim());

console.log(thVals)

<!-- language: lang-html -->

&lt;div id=&quot;TchmiRecipeEdit&quot; style=&quot;inset: 16px;&quot;&gt;
  &lt;div class=&quot; TCHES Controls Beckhoff TcHiRecipeEdit template tched-box &quot; tabindex=&quot;-1 &quot;&gt;
    &lt;div class=&quot;Tched Controls Beckhoff TcHnRecipeEdit-editing pane &quot;&gt;
      &lt;div class=&quot;TcHed_Controls Beckhoff TcHndRecipeEdit-editing-box&quot;&gt;
        &lt;div class=&quot;Tchad Controls Beckhoff TcHniRecipeEdit-editing-area&quot;&gt;
          &lt;table&gt;
            &lt;thead&gt;
              &lt;tr&gt;
                &lt;th&gt;Name&lt;/th&gt;
                &lt;th&gt;Value&lt;/th&gt;
                &lt;th&gt;in&lt;/th&gt;
                &lt;th&gt;Unit&lt;/th&gt;
              &lt;/tr&gt;
            &lt;/thead&gt;
          &lt;/table&gt;
        &lt;/div&gt;
      &lt;/div&gt;
    &lt;/div&gt;
  &lt;/div&gt;
&lt;/div&gt;

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年6月27日 21:36:29
  • 转载请务必保留本文链接:https://go.coder-hub.com/76565449.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定