通过<img/>点击切换Div类

时间:2016-03-29 11:33:29

标签: javascript jquery

您好,我试图通过点击按钮切换div的类但似乎无法正常工作,有什么建议吗?

HTML

<img id="downarrow" src="images/downarrow.svg"/>

<div class="responsive-subscribe">
</div>

CSS

.responsive-subscribe {
position: relative;
width: 100%;
height: auto;
background-color: ;
bottom: 10px;
display: none;
}

.open {
display: block;
}

JAVASCRIPT

$(document).ready(function() {
    $downarrow = $('#downarrow');

    $downarrow.click(function() {
        $(this).toggleClass('active');
        $('.responsive-subscribe').toggleClass('.open');
    });
});

2 个答案:

答案 0 :(得分:2)

$('.responsive-subscribe').toggleClass('.open');应该像$('.responsive-subscribe').toggleClass('open');

.

中删除.toggleClass('.open');

<强> Working Fiddle

答案 1 :(得分:0)

你有一个小错字,删除&#39;。&#39;来自.toggleClass:

JS

$(document).ready(function() {
    $downarrow = $('#downarrow');

    $downarrow.click(function() {
        $(this).toggleClass('active');
        $('.responsive-subscribe').toggleClass('open');
    });
});

演示: https://jsfiddle.net/0wcn5L7h/1/