如何更改< p id = [value] in html中的值
我以前曾问过这个问题的版本,非常感谢我得到的帮助,但我仍然卡住了,所以我认为最好问一个精致的问题,该问题现在可以到达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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
首先,我会修复您的HTML并确保您的ID唯一,然后您可以执行以下操作
First I would fix your html and make sure your ids are unique then you can do the following