简体   繁体   English

使用JavaScript在HTML中创建下拉复选框列表

[英]Create a drop down check box list in HTML with JavaScript

I need to create a drop down check box list like here . 我需要创建一个像这里的下拉复选框列表。 The problem with the code over there is that it is not working in IE9. 那里的代码存在的问题是,它在IE9中无法正常工作。 I'm getting an error saying getElementsByClassName is not a property in the below code: 我收到一条错误消息,指出以下代码中的getElementsByClassName不是属性:

var checkList = document.getElementById('list1');
var items = document.getElementById('items');
        checkList.getElementsByClassName('anchor')[0].onclick = function (evt) {
            if (items.classList.contains('visible')){
                items.classList.remove('visible');
                items.style.display = "none";
            }

            else{
                items.classList.add('visible');
                items.style.display = "block";
            }


        }

        items.onblur = function(evt) {
            items.classList.remove('visible');
        }

Can someone please help me out on how to create a drop down check box list? 有人可以帮我解决如何创建下拉复选框列表吗? Thanks. 谢谢。

The classList property is not available for an IE browser below 10. If you wanna do this you're gonna have to use className instead and check for the index of that class. classList属性不适用于10以下的IE浏览器。如果要执行此操作,则必须改用className并检查该类的索引。 For example: 例如:

if (items.className.indexOf('visible') > -1){
            items.className -= 'visible';
            items.style.display = "none";
        }

If your item has the visible class it will return its index and if it doesn't have that class it will return -1. 如果您的商品具有可见的类,它将返回其索引,如果没有该类,则将返回-1。 I have created a fiddle from yours here to demonstrate 我在你这里制造了一个小提琴来演示

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

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