如何更改< p id = [value] in html中的值

发布于 2025-02-01 03:50:42 字数 1159 浏览 2 评论 0原文

我以前曾问过这个问题的版本,非常感谢我得到的帮助,但我仍然卡住了,所以我认为最好问一个精致的问题,该问题现在可以到达Nub。

我有以下代码。这个想法是select命令应选择一个值(最初是XA,但可以是XB,XC,XD),然后将其替换为opent&lt; span; span行中的Xa,并导致适当的<代码> document.getElementById line in tor写作。

但这无法正常工作。谁能告诉我如何纠正它。

<span id="XA">Change</span>
<P>

  <select class="selector" onchange="document.querySelector('p').id=this.value; console.log(document.querySelector('p').id)">
    <option value="XA" selected>XA</option>
    <option value="XB">XB</option>
    <option value="XC">XC</option>
    <option value="XD">XD</option>
  </select>

  <p id="XA"></p>
  <p id="XB"></p>
  <p id="XC"></p>
  <p id="XD"></p>

  <script>
    document.getElementById("XA").innerHTML = "Option is XA";
    document.getElementById("XB").innerHTML = "Option is XB";
    document.getElementById("XC").innerHTML = "Option is XC";
    document.getElementById("XD").innerHTML = "Option is XD";
  </script>

I've asked a version of this question before and am very grateful for the help I've got but I'm still stuck so I thought it better to ask a refined question which now gets to the nub.

I've got the code below. The idea is that the select command should choose a value (initially XA, but could be XB, XC, XD) which will then be substituted for XA in the opening <span line and cause the appropriate document.getElementById line at the end to write.

But it doesn't work as it stands. Can anyone tell me how to correct it.

<span id="XA">Change</span>
<P>

  <select class="selector" onchange="document.querySelector('p').id=this.value; console.log(document.querySelector('p').id)">
    <option value="XA" selected>XA</option>
    <option value="XB">XB</option>
    <option value="XC">XC</option>
    <option value="XD">XD</option>
  </select>

  <p id="XA"></p>
  <p id="XB"></p>
  <p id="XC"></p>
  <p id="XD"></p>

  <script>
    document.getElementById("XA").innerHTML = "Option is XA";
    document.getElementById("XB").innerHTML = "Option is XB";
    document.getElementById("XC").innerHTML = "Option is XC";
    document.getElementById("XD").innerHTML = "Option is XD";
  </script>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

时光清浅 2025-02-08 03:50:42

首先,我会修复您的HTML并确保您的ID唯一,然后您可以执行以下操作

const span = document.getElementById('text');

document.querySelector('.selector')
  .addEventListener('change', event => {
    const thisValue = event.currentTarget.value;         // get current value
    const text = `Option is ${thisValue}`;
    span.innerText = text;                               // change span text
    document.getElementById(thisValue).innerText = text; // change p value
});
<span id="text">Change</span>

<select class="selector">
  <option value="XA" selected>XA</option>
  <option value="XB">XB</option>
  <option value="XC">XC</option>
  <option value="XD">XD</option>
</select>

<p id="XA"></p>
<p id="XB"></p>
<p id="XC"></p>
<p id="XD"></p>

First I would fix your html and make sure your ids are unique then you can do the following

const span = document.getElementById('text');

document.querySelector('.selector')
  .addEventListener('change', event => {
    const thisValue = event.currentTarget.value;         // get current value
    const text = `Option is ${thisValue}`;
    span.innerText = text;                               // change span text
    document.getElementById(thisValue).innerText = text; // change p value
});
<span id="text">Change</span>

<select class="selector">
  <option value="XA" selected>XA</option>
  <option value="XB">XB</option>
  <option value="XC">XC</option>
  <option value="XD">XD</option>
</select>

<p id="XA"></p>
<p id="XB"></p>
<p id="XC"></p>
<p id="XD"></p>

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文