jQuery e.preventDefault仅在第一次点击时不起作用

时间:2014-08-21 10:41:59

标签: javascript jquery html dom

我有防止按钮点击跳跃部分的问题。我的代码是:

脚本:

    $('.read-more').on('click', function (e) {
        e.preventDefault();
        $(".active").each(function () {
            $(this).removeClass("active");
        });
        var target = $(this).attr('data-toggle');
        $("#" + target).show().siblings("div").hide();
        $(this).closest("article").find(".read-more").each(function () {
            $(this).toggleClass('active');
        });
        $(this).closest("article").find(".arrow-marker").each(function () {
            $(this).toggleClass('active');
        });
        return false
    });

HTML:

    <article class="service">
        <a href="#" class="read-more" data-toggle="expand1"></a>
        <button class="read-more" data-toggle="expand1">Read More</button>
        <span class="arrow-marker"></span>
    </article>
    <article class="service">
        <a href="#" class="read-more" data-toggle="expand2"></a>
        <button class="read-more" data-toggle="expand2">Read More</button>
        <span class="arrow-marker"></span>
    </article>
    <article class="service">
        <a href="#" class="read-more" data-toggle="expand3"></a>
        <button class="read-more" data-toggle="expand3">Read More</button>
        <span class="arrow-marker"></span>
    </article>
    <article class="service">
        <a href="#" class="read-more" data-toggle="expand4"></a>
        <button class="read-more" data-toggle="expand4">Read More</button>
        <span class="arrow-marker"></span>
    </article>

    <div id="expand1"></div>
    <div id="expand2"></div>
    <div id="expand3"></div>
    <div id="expand4"></div>

此脚本的基本内容是切换4个选项卡和相关类。我需要并做同样的事情。

我必须说,它工作正常,只需首先点击任何标签跳转到页面顶部,然后每次点击都可以正常工作。你能找到代码有什么问题吗?我知道可能有更好的解决方案。

提前致谢!

3 个答案:

答案 0 :(得分:0)

我不完全确定它有用,但尝试使用.on就像这样:

$('body').on('click', '.read-more', function(){});

答案 1 :(得分:0)

<script>
$(document).ready(function() {
   $('.read-more').on('click', function (e) {
        e.preventDefault();
        $(".active").each(function () {
            $(this).removeClass("active");
        });
        var target = $(this).attr('data-toggle');
        $("#" + target).show().siblings("div").hide();
        $(this).closest("article").find(".read-more").each(function () {
            $(this).toggleClass('active');
        });
        $(this).closest("article").find(".arrow-marker").each(function () {
            $(this).toggleClass('active');
        });
        return false
    });
 });
</script>


   <article class="service">
        <a href="#" class="read-more" data-toggle="expand1"></a>
        <button class="read-more" data-toggle="expand1">Read More</button>
        <span class="arrow-marker"></span>
    </article>
    <article class="service">
        <a href="#" class="read-more" data-toggle="expand2"></a>
        <button class="read-more" data-toggle="expand2">Read More</button>
        <span class="arrow-marker"></span>
    </article>
    <article class="service">
        <a href="#" class="read-more" data-toggle="expand3"></a>
        <button class="read-more" data-toggle="expand3">Read More</button>
        <span class="arrow-marker"></span>
    </article>
    <article class="service">
        <a href="#" class="read-more" data-toggle="expand4"></a>
        <button class="read-more" data-toggle="expand4">Read More</button>
        <span class="arrow-marker"></span>
    </article>

    <div id="expand1">A Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum </div>
    <div style="clear:both; height:20px;"></div>
    <div id="expand2" style="display:none">B Sed ut perspiciatis unde omnis iste natus error sit voluptatem accusantium doloremque laudantium, totam rem aperiam, eaque ipsa quae ab illo inventore veritatis et quasi architecto beatae vitae dicta sunt explicabo. Nemo enim ipsam voluptatem quia voluptas sit aspernatur aut odit aut fugit, sed quia consequuntur magni dolores eos qui ratione voluptatem sequi nesciunt. Neque porro quisquam est, qui dolorem ipsum quia dolor sit amet, consectetur, adipisci velit, sed quia non numquam eius modi tempora incidunt ut labore et dolore magnam aliquam quaerat voluptatem. Ut enim ad minima veniam, quis nostrum exercitationem ullam corporis suscipit laboriosam, nisi ut aliquid ex ea commodi consequatur? Quis autem vel eum iure reprehenderit qui in ea voluptate velit esse quam nihil molestiae consequatur, vel illum qui dolorem eum fugiat quo voluptas nulla pariatur?</div>
    <div style="clear:both; height:20px;"></div>
    <div id="expand3" style="display:none">C Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum</div>
    <div style="clear:both; height:20px;"></div>
    <div id="expand4" style="display:none">D Sed ut perspiciatis unde omnis iste natus error sit voluptatem accusantium doloremque laudantium, totam rem aperiam, eaque ipsa quae ab illo inventore veritatis et quasi architecto beatae vitae dicta sunt explicabo. Nemo enim ipsam voluptatem quia voluptas sit aspernatur aut odit aut fugit, sed quia consequuntur magni dolores eos qui ratione voluptatem sequi nesciunt. Neque porro quisquam est, qui dolorem ipsum quia dolor sit amet, consectetur, adipisci velit, sed quia non numquam eius modi tempora incidunt ut labore et dolore magnam aliquam quaerat voluptatem. Ut enim ad minima veniam, quis nostrum exercitationem ullam corporis suscipit laboriosam, nisi ut aliquid ex ea commodi consequatur? Quis autem vel eum iure reprehenderit qui in ea voluptate velit esse quam nihil molestiae consequatur, vel illum qui dolorem eum fugiat quo voluptas nulla pariatur?</div>

DEMO:http://jsfiddle.net/anujsoft/rb7o8xxu/

答案 2 :(得分:0)

编辑:我找到了解决方案,问题出在课堂上.active。 Bootstrap旋转木马课程活跃,我已经针对页面上的所有活动课程...点击,旋转木马消失了,这导致跳跃...愚蠢的我。

感谢大家的帮助!!!