根据用户点击显示/隐藏div?

时间:2015-10-06 16:43:05

标签: javascript html events

我试图在用户点击google api地图上的侧边栏内的div后显示/隐藏某些div。 div设置为:

<div id="Map">
    <div id="sidebarContainer">
        <div id="sidebar">
            <div class="level1">
                <div class="level 2 primary [name of distributor]" id="[name of distributor]">
                [text data about the distributor in cased in some divs]
                </div>
            </div>
        </div>
    </div>
</div>                

然后是子公司:

<div class="level1">
<div class="level 2 subsidiary [name of distributor]" id="[name of distributor]" style="display:none">

我的目标是当用户点击主要div时,它将显示所有辅助div。主要和辅助div都在同一个&#39;结构中的水平,根据经销商而言,根本没有类别。我试过这个javascript:

google.maps.event.addListener(
    document.getElementsByClassName('level 2 primary ' + listingInfo.PARTY_NAME),
    'click',
    function() {
        console.log(listingInfo.PARTY_NAME + ' INSIDE THE CLICKY CLICKY');
        document.getElementsByClassName(listingInfo.PARTY_NAME)
            .style.display = 'inline-block';
    }
);

listingInfo.PARTY_NAME = [name of the distributor]

我也试过在1级div上做听众,但这似乎不起作用。我还没有能够显示我的日志消息,但我无法弄清楚我做错了什么。任何帮助表示赞赏!

1 个答案:

答案 0 :(得分:0)

<div class="level 2 subsidiary [name of distributor]" id="[name of distributor]" style="display:none">

在代码的上一行class中,名称为level 2 subsidiary [name of distributor],其中包含空格,因此它表示div具有名为level2的不同类。因此,您的代码将无法按预期工作。

尝试使用单词更改类名。

相关问题