javascript - 需要从 get ByID 更改为 ClassName (Javascript)

标签 javascript php html css

当我使用 getElementByID 执行此操作时,它正在工作。但是当我将其更改为 className 时,它​​不起作用。

我之所以要用类名来做这个,是因为页面中会有很多循环 (php) 票,这些票是按票号排序的。当点击特定票的“查看”时,它会显示完整的消息。

当然,我这里是无法通过多个重复的“id”获取到的。我只需要将它更改为类名。请帮助。

            <div id="menuTiket">
                <span style="padding:10px; background:yellow; float:right;">
                    <a href = "javascript:void(0)" onclick ="document.getElementsByClassName('light').style.display='block'; 
                                                             document.getElementsByClassName('fade').style.display='block';
                                                             ">Tiket Baru</a></span></div>

                    <!--Black Overlay-->    
                    <div id="fade" class="fade overlayMessage" onLoad="initDynamicOptionLists()"></div>                                        

                    <!--Pop Up Div-->   
                    <div id="light" class="light popupBoxMesage">                      

                            <span style="position: absolute; top: 11px; right:1px; color:white;" id="closeBlocked">
                                <a style="color:green; text-decoration:none; background:white; padding:10px;" href = "javascript:void(0)" onclick ="document.getElementsByClassName('light').style.display='none'; document.getElementsByClassName('fade').style.display='none'"><b> X </b></a>
                            </span>
                        </div> 

CSS:

    <style type="text/css">

            .overlayMessage{
                display: none;
                position: fixed;
                top: 0%;
                left: 0%;
                width: 100%;
                height: 100%;
                background-color: black;
                z-index:10000001;
                -moz-opacity: 0.5;
                opacity:.45;
                filter: alpha(opacity=40);
            }

            .popupBoxMesage {
                display: none;
                position: fixed;
                top: 0;
                left: 0;
                bottom:0;
                right:0;
                border: solid 10px darkseagreen;
                background-color: greenyellow;
                width:70%;
                height:400px;
                z-index:10000002;
                overflow: auto;
                padding: 1% 2% 12px 2%;
                margin: auto;
            }
            /* CSS Document */
    </style>

最佳答案

它是 getElementsByClassName(注意“Element”之后的“s”)因为它返回多个元素。 DOM 中可以有多个具有相同类的元素。

结果将是一个集合,您可以像这样看到一个特定的元素:

var elements = document.getElementsByClassName("class");
var firstElement = elements[0];

关于javascript - 需要从 get ByID 更改为 ClassName (Javascript),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26911801/

相关文章:

javascript - Vue如何将for循环按钮表分成表中的不同行

javascript - Jquery ajax 相当于钛

javascript - 获取 HTML 表中按下按钮的行

php - 关联数组

php - 为什么我的文件在尝试使用 PHP 读取时总是为空?

html - 为什么我的文字折叠成 4 行?

javascript - 定期更新和渲染来自 Flask 的值

javascript - 如何为 Webix 数据表的 subview 设置 ID

php - 如何将文件内容复制到 PHP 中的临时文件中?

javascript - 手动添加到 HTML 范围输入会产生意外结果