简体   繁体   English

单击其他div链接时关闭div jquery

[英]Close div when other div link is clicked jquery

Need some help closing an expanding div when a link to open another expanding div is clicked. 当点击打开另一个扩展div的链接时,需要一些帮助关闭扩展div

I've tried using examples from other functions but I don't have a good enough understanding to alter it to work. 我尝试过使用其他函数的例子,但我没有足够的理解来改变它的工作。

Here's the HTML: 这是HTML:

<div class="service" id="service-box1">
    <a class="learn-more" id="show1" data-href="service1">Learn More</a>
</div>
<div class="service" id="service-box2">
    <a class="learn-more" id="show2" data-href="service2">Learn More</a>
</div>
<div class="service-content" id="service1">
    <p>Title</p>
</div>
<div class="service-content" id="service2">
    <p>Title</p>
</div>

Here's the function: 这是功能:

$('document').ready(function() {
    $('#show1').click(function() {
        var $div = $('#' + $(this).data('href'));
        $('#serivce1').not($div).hide();
        $div.slideToggle();
    });
});

$('document').ready(function() {
    $('#show2').click(function() {
        var $div = $('#' + $(this).data('href'));
        $('#serivce2').not($div).hide();
        $div.slideToggle();
    });
});

So if the #show1 button is clicked and displays the #service1 div then the #show2 button is clicked, the #service1 div should close and open the #service2 div . 因此,如果单击#show1按钮并显示#service1 div则单击#show2按钮, #service1 div应关闭并打开#service2 div

I think you can try to use onclick function. 我想你可以尝试使用onclick功能。

HTML HTML

<div class="service" id="service-box1">
    <a class="learn-more" id="show1" data-href="service1" onclick="showservice1()">Learn More</a>
</div>

<div class="service" id="service-box2">
    <a class="learn-more" id="show2" data-href="service2" onclick="showservice2()">Learn More</a>
</div>

JQUERY/JS JQUERY / JS

function showservice1() {
    $('#service1').show(100);
    $('#service2').hide(100);
}

function showservice2() {
    $('#service1').show(100);
    $('#service2').hide(100);
}

And 1 more thing... 还有一件事......

I saw you TYPO where you declare ID as "service1" and "service2" but in jquery you call "serivce1" and "serivce2". 我看到你在TYPO中将ID声明为“service1”和“service2”但在jquery中你称为“serivce1”和“serivce2”。 That's why it wont work 这就是为什么它不会工作

Hopefully this help you 希望这能帮到你

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

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