Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/78.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 为什么在使用getElementById时不能更改显示样式?_Javascript_Html_Css_Runtime Error_Getelementbyid - Fatal编程技术网

Javascript 为什么在使用getElementById时不能更改显示样式?

Javascript 为什么在使用getElementById时不能更改显示样式?,javascript,html,css,runtime-error,getelementbyid,Javascript,Html,Css,Runtime Error,Getelementbyid,我的javascript函数无法正常工作,以下是相关代码: <script> function sState(elmntC,elmntO) { elmntC=document.getElementById(elmntC); elmntO=document.getElementById(elmntO); elmntC.style.display="none"; elmntO.style.display="block"

我的javascript函数无法正常工作,以下是相关代码:

<script>
function sState(elmntC,elmntO) {
    elmntC=document.getElementById(elmntC);
    elmntO=document.getElementById(elmntO);
    elmntC.style.display="none";
    elmntO.style.display="block";
}
</script>
以及调用函数的主体中的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>
但是,当我运行代码并触发onClick事件时,在Inspect元素中出现以下错误:

Degities.html:21未捕获类型错误:无法读取的属性“style” 无效的 在州神学院。html:21 在HTMLButtonElement.onclick degities.html:35

dediges.html:21表示elmntC.style.display=none; html:35是指按钮onClick函数调用

这是怎么回事?为什么我会出现这个错误,我该如何修复这个错误?已经尝试将脚本移动到正文底部,但没有效果

此外,如果有人有任何想法可以更有效地执行此操作,即避免使用getElementById调用,请让我知道


另外,对于那些好奇的人来说,这是为了在DnD中为我的玩家建立一个神搜索工具——我是一个普通的DM,我认为当我的玩家想扮演牧师时,这会让他们的生活更轻松。在第一个条目开始工作后,我将开始实现类来锚定搜索。

在html页面上调用sState函数时出现问题

您需要将onClick=sStateaegirSimp、aegirExp替换为onClick=sState'aegirSimp'、'aegirExp'以将id值发送到sState函数

函数sstatelmntc,elmntO{ elmntC=document.getElementByIdelmntC; elmntO=document.getElementByIdelmntO; elmntC.style.display=none; elmntO.style.display=block; } 埃吉尔 氖 海神与风暴 大风暴 + AegirCG海洋和风暴之神领域:风暴符号:汹涌的海浪传说中,Aegir居住在海底的一个巨大大厅里


错误在于调用函数时传递ID的方式

ID应作为字符串传递,如下所示

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

根据错误消息,它没有识别元素并返回null,请尝试控制台,查看方法中的数据是否正常。console.logelmntC,elmnt在设置样式之前。谢谢,我认为这不起作用,因为当我最初在onClick引号中使用getElementById时,引号将其弄糟。谢谢,我认为这不起作用,因为当我最初在onClick引号中使用getElementById时,引号将其弄糟。