getElementById innerHTML 未定义

getElementById innerHTML undefined

当我在同一个 <div class> 中有 item1item2 时,item2 returns worlditem1 returns undefined.

function Delivered(xCode, tNo) {
    document.getElementById("item1").innerHTML = "hello";
    document.getElementById("item2").innerHTML = "world";
}

但是,如果我只是删除 <span id="item2"></span>item1 会再次报告 hello

<div id="details"> 
    <div class="right" id="rightDIV">content...<span id="item1"></span>..inserted here</p>
    <p> more content...<span id="item2"></span>
    </div>
</div>

我尝试将 item2 放入新的 <div class> 中,但仍然没有成功。我做错了什么?

编辑:我可以 post 原始代码吗?抱歉,先 post.

<div id="details"> 
    <div class="left" id="leftDIV">item: <b><span id="item3"></span></b>
    <p>via <b>airmail;</b></p>
    </div>
    <div class="right" id="rightDIV">Status: <b>active</b>
        <p>item with reference <span id="item1"></span> content here <span id="item2"></span></p>
        <p>Thank you&trade;</p>
    </div>
</div>

这是我的脚本:

#details { 
  height:200px; 
  width:600px; 
  font-family: Tahoma;
  color: #000; 
  margin: 0px;
  font-size: 12px; 
}
#details .left{ 
  width:33%; 
  height:200px; 
  padding: 15px; 
  background-color: #fff; 
  float: left;
  overflow:hidden;
  visibility: hidden;
}
#details .right{ 
  width:40%;
  height:200px; 
  padding: 15px; 
  background-color: #ccc; 
  float: left;
  overflow:hidden;
  visibility: hidden;
}

function Delivered(xCode, tCode) {
  document.getElementById("leftDIV").style.visibility = "visible";
  document.getElementById("rightDIV").style.visibility = "visible";
  document.getElementById("item3").innerHTML = tCode;
  document.getElementById("item1").innerHTML = tCode;
  document.getElementById("item2").innerHTML = xCode;
}

我不确定要粘贴多少代码,因为我不确定相关内容:)

你写完js代码忘记调用函数了。

function Delivered(xCode, tNo) {
    document.getElementById("item1").innerHTML = "hello";
    document.getElementById("item2").innerHTML = "world";
} 

Delivered();

并且在 html 中你关闭了 p 元素但你从未打开过

 <div id="details"> 
   <div class="right" id="rightDIV">
    content...<span id="item1"> </span>..inserted here</p> -----> Here
    you opened but never closed <--------- <p>more content...<span id="item2"></span>
    </div>
</div>

在Js中这样做

function delivered(xCode, tNo) {
    document.getElementById("item1").innerHTML = "hello";
    document.getElementById("item2").innerHTML = "world";
}

delivered();

并且html像这样

<div id="details"> 
   <div class="right" id="rightDIV"><p>content...<span id="item1"></span>..inserted here</p>
       <p> more content...<span id="item2"></span></p>
   </div>
</div>