简体   繁体   English

HTML JavaScript document.getElementById

[英]HTML JavaScript document.getElementById

First, my English not very well when you read this post, I'm sorry.首先,当你阅读这篇文章时,我的英语不是很好,我很抱歉。

Recently I met one problem, I try to get table ID from buttons send the ID to function do compare.最近我遇到了一个问题,我尝试从按钮中获取表 ID,将 ID 发送到函数进行比较。 And in the function, I have created an Array to stored the table ID.在函数中,我创建了一个数组来存储表 ID。 But I can't successfully compare Id used array or string.但我无法成功比较 Id 使用的数组或字符串。

Can somebody teach me how to solve or do this thing?有人可以教我如何解决或做这件事吗?

Here is my code HTML: HTML这是我的代码 HTML: HTML
JS: JavsScrpit JS: JavsScrpit

 var arrTabID = ["tSec1", "tSec2", "tSec3", "tSec4", "tSec5", "tSec6", "tSec7"]; function displaySetting(id) { /*var tabID = document.getElementById(id);*/ /*if(document.getElementById(id)==arrTabID[0])*/ if (document.getElementById(id) == "tSec1") { if (document.getElementById(id).style.display == "none") { doucment.getElementById(id).style.display == "block"; var i; for (i = 0; i < 7; i++) { if (i != 0) { document.getElementById(id).style.display = "none"; } } } else { document.getElementById(id).style.display = "block"; } } /*if(document.getElementById(id)==arrTabID[1])*/ else if (document.getElementById(id) == "tSec2") { if (document.getElementById(id).style.display == "none") { document.getElementById(id]).style.display = "block"; var i; for (i = 0; i < 7; i++) { if (i != 0) { document.getElementById(id).style.display = "none"; } } } else { document.getElementById(id).style.display = "block"; } } /*if(document.getElementById(id)==arrTabID[2])*/ else if (document.getElementById(id) == "tSec3") { if (document.getElementById(id).style.display == "none") { document.getElementById(id).style.display = "block"; var i; for (i = 0; i < 7; i++) { if (i != 0) { document.getElementById(id).style.display = "none"; } } } else { document.getElementById(id).style.display = "block"; } } }
 <table class="table-content"> <tbody> <!--Section 1--> <div> <table id="tSec1" name="NtSec1" style="display:block;" height="450px"> <tbody> <tr> <td style="text-align:left;"><br> <img src="../pic/voiceIcon.png" alt="voiceIcon" height="50" width="50"> <audio controls> <source src="../sound/media_1/media1.mp3" type="audio/mpeg"> </audio> </td> </tr> <tr> <td> <h3> 1.</h3> </td> </tr> <tr> <td> <h3> 2.</h3> </td> </tr> <tr> <td> <h3> 3. </h3> </td> </tr> <tr> <td> <h3> 4. </h3> </td> </tr> <tr> <td> <h3> 5.</h3> </td> </tr> <tr> <td> <h3>&sect;.</h3> </td> </tr> </tbody> </table> </div> <!--Section 2--> <div> <table id="tSec2" name="NtSec2" style="display:none;" height="450px"> <tbody> <tr> <td style="text-align:left;"> <img src="../pic/voiceIcon.png" alt="voiceIcon" height="55" width="55"> <audio controls> <source src="../sound/media_1/media2.mp3" type="audio/mpeg"> </audio> </td> </tr> <tr> <td> <h3> 6.</h3> </td> </tr> <tr> <td> <h3>&sect;</h3> </td> </tr> <tr> <td> <h3> 7.</h3> </td> </tr> <tr> <td> <h3>&sect;</h3> </td> </tr> <tr> <td> <h3> 8.</h3> </td> </tr> <tr> <td> <h3>&sect; </h3> </td> </tr> <tr> <td> <h3> 9.</h3> </td> </tr> </tbody> </table> </div> <!--Section 3--> <div> <table id="tSec3" name="NtSec3" style="display:none;" height="450px"> <tbody> <tr> <td class="hidden-phone" style="text-align:left;"><br> <img src="../pic/voiceIcon.png" alt="voiceIcon" height="50" width="50"> <audio controls> <source src="../sound/media_1/media3.mp3" type="audio/mpeg"> </audio> </td> </tr> <tr> <td> <h3> 10.</h3> </td> </tr> <tr> <td> <h3> 11.</h3> </td> </tr> <tr> <td> <h3>&sect;</h3> </td> </tr> <tr> <td> <h3> 12.</h3> </td> </tr> <tr> <td> <h3> 13.</h3> </td> </tr> <tr> <td> <h3>&sect;</h3><br></td> </tr> </tbody> </table> </div> <!--Section 4--> <div> <table id="tSec4" name="NtSec4" style="display:none;" height="450px"> <tbody> <tr> <td class="hidden-phone" style="text-align:left;"> <img src="../pic/voiceIcon.png" alt="voiceIcon" height="55" width="55"> <audio controls> <source src="../sound/media_1/media4.mp3" type="audio/mpeg"> </audio> </td> </tr> <tr> <td> <h3> 14.</h3> </td> </tr> <tr> <td> <h3>&sect;</h3> </td> </tr> <tr> <td> <h3> 15.</h3> </td> </tr> <tr> <td> <h3> 16.</h3> </td> </tr> <tr> <td> <h3> 17.</h3> </td> </tr> </tbody> </table> </div> <!--Section 5--> <div> <table id="tSec5" name="NtSec5" style="display:none;" height="450px"> <tbody> <tr> <td style="text-align:left;"> <img src="../pic/voiceIcon.png" alt="voiceIcon" height="55" width="55"> <audio controls> <source src="../sound/media_1/media5.mp3" type="audio/mpeg"> </audio> </td> </tr> <tr> <td> <h3> 18.</h3> </td> </tr> <tr> <td> <h3> 19.</h3> </td> </tr> <tr> <td> <h3> 20. </h3> </td> </tr> <tr> <td> <h3> 21.</h3><br></td> </tr> </tbody> </table> </div> <!--Section 6--> <div> <table id="tSec6" name="NtSec6" style="display:none;" height="450px"> <tbody> <tr> <td style="text-align:left;"> <img src="../pic/voiceIcon.png" alt="voiceIcon" height="55" width="55"> <audio controls> <source src="../sound/media_1/media6.mp3" type="audio/mpeg"> </audio> </td> </tr> <tr> <td> <h3> 22.</h3> </td> </tr> <tr> <td> <h3> &sect; </h3> </td> </tr> <tr> <td> <h3> 23.</h3> </td> </tr> <tr> <td> <h3>&sect;</h3> </td> </tr> <tr> <td> <h3> 24.</h3> </td> </tr> <tr> <td> <h3> 25. </h3> </td> </tr> </tbody> </table> </div> <!--Section 7--> <div> <table id="tSec7" name="NtSec7" style="display:none;" height="450px"> <tbody> <tr> <td style="text-align:left;"> <img src="../pic/voiceIcon.png" alt="voiceIcon" height="55" width="55"> <audio controls> <source src="../sound/media_1/media7.mp3" type="audio/mpeg"> </audio> </td> </tr> <tr> <td> <h3> 26.</h3> </td> </tr> <tr> <td> <h3>&sect;</h3> </td> </tr> <tr> <td> <h3> 27.</h3> </td> </tr> <tr> <td> <h3> 28.</h3> </td> </tr> <tr> <td> <h3> 29.</h3> </td> </tr> </tbody> </table> </div> <div> <h1>Page</h1>&nbsp;&nbsp; <button style="height:50px;width:50px;font-size:30px;" onclick="displaySetting('tSec1')">1</button>&nbsp; <button style="height:50px;width:50px;font-size:30px;" onclick="displaySetting('tSec2')">2</button>&nbsp; <button style="height:50px;width:50px;font-size:30px;" onclick="displaySetting('tSec3')">3</button>&nbsp; <button style="height:50px;width:50px;font-size:30px;" onclick="displaySetting('tSec4')">4</button>&nbsp; <button style="height:50px;width:50px;font-size:30px;" onclick="displaySetting('tSec5')">5</button>&nbsp; <button style="height:50px;width:50px;font-size:30px;" onclick="displaySetting('tSec6')">6</button>&nbsp; <button style="height:50px;width:50px;font-size:30px;" onclick="displaySetting('tSec7')">7</button> </div> </tbody> </table>

Thank you for your help.感谢您的帮助。 Best regard.最良好的问候。

You compare an HTML object and a string.您比较一个 HTML 对象和一个字符串。

if(document.getElementById(id)=="tSec1")

Use if(id == 'tSec1') instead.使用if(id == 'tSec1')代替。

And then there is a typo in然后有一个错字

document.getElementById(id]).style.display = "block";

and your loops do nothing.而你的循环什么都不做。

If you want the function to set display to block for an element with given id and set all other elements to display none you could do it like this:如果你想要的功能,以设置的显示,以带指定id的元素,并将所有其他元素,以显示没有,你可以做这样的:

 var arrTabID = ["tSec1", "tSec2", "tSec3", "tSec4", "tSec5", "tSec6", "tSec7"]; function displaySetting(id) { var tabID; var index = arrTabID.indexOf(id); // index of given id in array arrTabID for (var i = 0; i < arrTabID.length; i += 1) { tabID = document.getElementById(arrTabID[i]); // for ids in arrTabID if (i == index) { tabID.style.display = 'block'; } else { tabID.style.display = 'none'; } } }
 <table class="table-content"> <tbody> <!--Section 1--> <div> <table id="tSec1" name="NtSec1" style="display:block;" height="450px"> <tbody> <tr> <td style="text-align:left;"><br> <img src="../pic/voiceIcon.png" alt="voiceIcon" height="50" width="50"> <audio controls> <source src="../sound/media_1/media1.mp3" type="audio/mpeg"> </audio> </td> </tr> <tr> <td> <h3> 1.</h3> </td> </tr> <tr> <td> <h3> 2.</h3> </td> </tr> <tr> <td> <h3> 3. </h3> </td> </tr> <tr> <td> <h3> 4. </h3> </td> </tr> <tr> <td> <h3> 5.</h3> </td> </tr> <tr> <td> <h3>&sect;.</h3> </td> </tr> </tbody> </table> </div> <!--Section 2--> <div> <table id="tSec2" name="NtSec2" style="display:none;" height="450px"> <tbody> <tr> <td style="text-align:left;"> <img src="../pic/voiceIcon.png" alt="voiceIcon" height="55" width="55"> <audio controls> <source src="../sound/media_1/media2.mp3" type="audio/mpeg"> </audio> </td> </tr> <tr> <td> <h3> 6.</h3> </td> </tr> <tr> <td> <h3>&sect;</h3> </td> </tr> <tr> <td> <h3> 7.</h3> </td> </tr> <tr> <td> <h3>&sect;</h3> </td> </tr> <tr> <td> <h3> 8.</h3> </td> </tr> <tr> <td> <h3>&sect; </h3> </td> </tr> <tr> <td> <h3> 9.</h3> </td> </tr> </tbody> </table> </div> <!--Section 3--> <div> <table id="tSec3" name="NtSec3" style="display:none;" height="450px"> <tbody> <tr> <td class="hidden-phone" style="text-align:left;"><br> <img src="../pic/voiceIcon.png" alt="voiceIcon" height="50" width="50"> <audio controls> <source src="../sound/media_1/media3.mp3" type="audio/mpeg"> </audio> </td> </tr> <tr> <td> <h3> 10.</h3> </td> </tr> <tr> <td> <h3> 11.</h3> </td> </tr> <tr> <td> <h3>&sect;</h3> </td> </tr> <tr> <td> <h3> 12.</h3> </td> </tr> <tr> <td> <h3> 13.</h3> </td> </tr> <tr> <td> <h3>&sect;</h3><br></td> </tr> </tbody> </table> </div> <!--Section 4--> <div> <table id="tSec4" name="NtSec4" style="display:none;" height="450px"> <tbody> <tr> <td class="hidden-phone" style="text-align:left;"> <img src="../pic/voiceIcon.png" alt="voiceIcon" height="55" width="55"> <audio controls> <source src="../sound/media_1/media4.mp3" type="audio/mpeg"> </audio> </td> </tr> <tr> <td> <h3> 14.</h3> </td> </tr> <tr> <td> <h3>&sect;</h3> </td> </tr> <tr> <td> <h3> 15.</h3> </td> </tr> <tr> <td> <h3> 16.</h3> </td> </tr> <tr> <td> <h3> 17.</h3> </td> </tr> </tbody> </table> </div> <!--Section 5--> <div> <table id="tSec5" name="NtSec5" style="display:none;" height="450px"> <tbody> <tr> <td style="text-align:left;"> <img src="../pic/voiceIcon.png" alt="voiceIcon" height="55" width="55"> <audio controls> <source src="../sound/media_1/media5.mp3" type="audio/mpeg"> </audio> </td> </tr> <tr> <td> <h3> 18.</h3> </td> </tr> <tr> <td> <h3> 19.</h3> </td> </tr> <tr> <td> <h3> 20. </h3> </td> </tr> <tr> <td> <h3> 21.</h3><br></td> </tr> </tbody> </table> </div> <!--Section 6--> <div> <table id="tSec6" name="NtSec6" style="display:none;" height="450px"> <tbody> <tr> <td style="text-align:left;"> <img src="../pic/voiceIcon.png" alt="voiceIcon" height="55" width="55"> <audio controls> <source src="../sound/media_1/media6.mp3" type="audio/mpeg"> </audio> </td> </tr> <tr> <td> <h3> 22.</h3> </td> </tr> <tr> <td> <h3> &sect; </h3> </td> </tr> <tr> <td> <h3> 23.</h3> </td> </tr> <tr> <td> <h3>&sect;</h3> </td> </tr> <tr> <td> <h3> 24.</h3> </td> </tr> <tr> <td> <h3> 25. </h3> </td> </tr> </tbody> </table> </div> <!--Section 7--> <div> <table id="tSec7" name="NtSec7" style="display:none;" height="450px"> <tbody> <tr> <td style="text-align:left;"> <img src="../pic/voiceIcon.png" alt="voiceIcon" height="55" width="55"> <audio controls> <source src="../sound/media_1/media7.mp3" type="audio/mpeg"> </audio> </td> </tr> <tr> <td> <h3> 26.</h3> </td> </tr> <tr> <td> <h3>&sect;</h3> </td> </tr> <tr> <td> <h3> 27.</h3> </td> </tr> <tr> <td> <h3> 28.</h3> </td> </tr> <tr> <td> <h3> 29.</h3> </td> </tr> </tbody> </table> </div> <div> <h1>Page</h1>&nbsp;&nbsp; <button style="height:50px;width:50px;font-size:30px;" onclick="displaySetting('tSec1')">1</button>&nbsp; <button style="height:50px;width:50px;font-size:30px;" onclick="displaySetting('tSec2')">2</button>&nbsp; <button style="height:50px;width:50px;font-size:30px;" onclick="displaySetting('tSec3')">3</button>&nbsp; <button style="height:50px;width:50px;font-size:30px;" onclick="displaySetting('tSec4')">4</button>&nbsp; <button style="height:50px;width:50px;font-size:30px;" onclick="displaySetting('tSec5')">5</button>&nbsp; <button style="height:50px;width:50px;font-size:30px;" onclick="displaySetting('tSec6')">6</button>&nbsp; <button style="height:50px;width:50px;font-size:30px;" onclick="displaySetting('tSec7')">7</button> </div> </tbody> </table>

What I get from your post is.. you are trying to search in array of table id's, if exist you want to perform some action我从您的帖子中得到的是.. 您正在尝试在表 ID 数组中进行搜索,如果存在您想要执行某些操作

you can do this by code given below.你可以通过下面给出的代码来做到这一点。

var arrTabID = ["tSec1", "tSec2", "tSec3", "tSec4", "tSec5", "tSec6", "tSec7"];

function displaySetting(id) {
  var tabID = document.getElementById(id);
 debugger
  if (id == arrTabID.find(x=>x==id)) {
    if (tabID.style.display == "none") {
      tabID.style.display == "block";
      var i;
      for (i = 0; i < 7; i++) {
        if (i != 0) {
          tabID.style.display = "none";
        }
      }
    } else {
      tabID.style.display = "block";
    }
  }

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM