简体   繁体   English

为什么在使用 getElementById 时无法更改显示样式?

[英]Why can't I change display style when using getElementById?

I'm having trouble getting my javascript function to work, here's the relevant code:我在让我的 javascript 函数工作时遇到问题,这是相关的代码:

<script>
function sState(elmntC,elmntO) {
    elmntC=document.getElementById(elmntC);
    elmntO=document.getElementById(elmntO);
    elmntC.style.display="none";
    elmntO.style.display="block";
}
</script>

And the HTML in the body which is calling the function:以及调用该函数的正文中的 HTML:

<div id="aegir">
    <table id="aegirSimp">
        <tr>
            <th class="name">Aegir</th>
            <th class="alignment">NE</th>
            <th class="title">God of the Sea and Storms</th>
            <th class="domains">Tempest</th>
            <th class="button"><button type="button" onClick="sState(aegirSimp,aegirExp)">+</button></th>
        </tr>
    </table>
    <p id="aegirExp">Aegir<br>CG god of the Sea and Storms<br>Domains: Tempest<br>Symbol: Rough ocean waves<br><br>Norse god of storms and seas, Aegir dwells in an immense hall at the bottom of the seas according to legend.<br><button type="button" id="aegirBC">x</button></p>
</div>

However, when I run the code and trigger the onClick event, I get the following error in Inspect Element:但是,当我运行代码并触发 onClick 事件时,在 Inspect Element 中出现以下错误:

deities.html:21 Uncaught TypeError: Cannot read property 'style' of null at sState (deities.html:21) at HTMLButtonElement.onclick (deities.html:35) deities.html:21 未捕获的类型错误:无法在 HTMLButtonElement.onclick (deities.html:35) 的 sState (deities.html:21) 处读取 null 的属性“样式”

deities.html:21 refers to elmntC.style.display="none"; deities.html:21 指的是 elmntC.style.display="none"; deities.html:35 refers to the button onClick function call. deities.html:35 指的是按钮 onClick 函数调用。

What's going on here?这里发生了什么? Why am I getting this error, and how can I fix this?为什么我会收到此错误,我该如何解决? Already tried moving the script to the bottom of the body, to no avail.已经尝试将脚本移动到正文的底部,但无济于事。

Also, if anyone has any ideas to do this more efficiently (ie by avoiding a getElementById call), please let me know.此外,如果有人有任何想法可以更有效地执行此操作(即通过避免 getElementById 调用),请告诉我。

PS for those curious, the purpose of this is to build a deity search tool for my players in DnD - I'm a regular DM, and thought it would make my players' lives easier when they want to play a cleric. PS 对于那些好奇的人,这样做的目的是为 DnD 中的玩家构建一个神灵搜索工具 - 我是一个普通的 DM,并认为当他们想要扮演牧师时,这会让我的玩家的生活更轻松。 Gonna start implementing classes to anchor the searches later, after I get the first entry working.在我获得第一个条目后,我将开始实施类来锚定搜索。

There is a problem on calling sState function on html page.在 html 页面上调用sState函数有问题。

You need to replace onClick="sState(aegirSimp,aegirExp)" to onClick="sState('aegirSimp','aegirExp')" to send the id values to sState function.您需要将onClick="sState(aegirSimp,aegirExp)"替换为onClick="sState('aegirSimp','aegirExp')"以将 id 值发送到sState函数。

 function sState(elmntC,elmntO) { elmntC=document.getElementById(elmntC); elmntO=document.getElementById(elmntO); elmntC.style.display="none"; elmntO.style.display="block"; }
 <div id="aegir"> <table id="aegirSimp"> <tr> <th class="name">Aegir</th> <th class="alignment">NE</th> <th class="title">God of the Sea and Storms</th> <th class="domains">Tempest</th> <th class="button"><button type="button" onClick="sState('aegirSimp','aegirExp')">+</button></th> </tr> </table> <p id="aegirExp">Aegir<br>CG god of the Sea and Storms<br>Domains: Tempest<br>Symbol: Rough ocean waves<br><br>Norse god of storms and seas, Aegir dwells in an immense hall at the bottom of the seas according to legend.<br><button type="button" id="aegirBC">x</button></p> </div>

Error is in the way you are passing ids while calling your function错误在于您在调用函数时传递 id 的方式

Ids should be passed as string as below Id 应该作为字符串传递,如下所示

 <th class="button"><button type="button" onClick="sState('aegirSimp','aegirExp')">+</button></th>

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

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