本文介绍了如何防止窗口在jQuery slideToggle()上向上滚动?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用jQuery在页面加载时动态添加两个div。这一切都很好,除了,当我点击第一个动态加载的div中的高级选项链接时 - 它执行它的正确行为(滑动第二个动态div),但它重置滚动的窗口回到顶部。我试图专注于我滑开的div的第一个输入元素,但无济于事。

I'm using jQuery to dynamically add two divs on page load. It all works great, except, when I click the "Advanced Options" link in the first dynamically loaded div - it performs it's correct behavior (sliding the second dynamic div), BUT it resets the scroll of the window back to the top. I attempted to focus on the first input element of the div that I slide open, but to no avail.

如何摆脱这种不受欢迎的滚动行为?

How do I get rid of this undesired scrolling behavior?

我已经包含了帮助的代码(尽可能长的啰嗦)

I've included the code to help (as long winded as it is)

<style type="text/css">
.itemcontainer {
    background-color:#FFFFCC;
    border:1px solid #999999;
    height:auto;
    margin:1em 0;
    overflow:hidden;
    position:relative;
    width:600px;
    }
.itemcontainer .optionpopoutcontainer {
    background-color:#44CC55;
    bottom:0;
    color:#333333;
    height:auto;
    left:0;
    padding:6px;
    position:absolute;
    width:100%;
}
.itemcontainer .advancedpopoutcontainer {}
</style>

这里是javascript以及HTML

And here's the javascript along with the HTML

    <script type="text/javascript">
    $(document).ready(function(){
        var counter = 0;
        //Add the necessary divs to the classes
        $(".itemcontainer").each(function(){
            var id = $(this).find("input[name='ItemID']").attr('value');
            $(this).append("<div class='optionpopoutcontainer' id='quickaddcontainer-" + id + "'></div><div class='advancedpopoutcontainer' id='advancedaddcontainer-" + id + "'></div>");
        });


            $(".optionpopoutcontainer").css("display", "none"); //used for the sliding up
        $(".optionpopoutcontainer").each(function(){
            createQuickAddForm($(this), GetID($(this)));
        });
        $(".advancedpopoutcontainer").css("display", "none"); //used for the sliding up
        $(".advancedpopoutcontainer").each(function(){
            AddAdvancedOptionsForm($(this), GetID($(this)));
        });


        $(".cancelButton").click(function(){
            var parent = GetParentDiv($(this));
            parent.slideToggle();
        });


        $(".opencontainer").click(function(){
            GetParentDiv($(this)).find("div.optionpopoutcontainer").slideToggle(250);
        });

        $(".quickaddadvoptions").click(function(){
            var container = GetParentDiv($(this));
            $(container).slideToggle(function(){
                var innerContainer = GetParentDiv(container).find("div.advancedpopoutcontainer").slideToggle(250);

            });
        });

        function GetParentDiv(item){
            return item.parents('div:eq(0)');
        }

        function GetID(item){
            return item.attr('id').split('-')[1];
        }


        $("input[name='Date']").each(function(){
            $(this).datepicker({dateFormat: 'd MM, y'});
        });
    });



    function createQuickAddForm(item, itemID){
            var str = "<form method='post' action='#' id='addform-" + itemID + "'>";
            str += "Size: <input id='Size' type='text' value='1' name='Size'/> ";
            str += "<input id='ItemID' type='hidden' value='29' name='ItemID'/>";
            str += "<input type='submit' value='Log Item'/>";
            str += "<a href='#'' class='quickaddadvoptions' id='advancedoptions-" + itemID + "'>Advanced Options</a>";
            str += "</form>";
            str += "<button  class='cancelButton'>Cancel</button>";

            item.html(str);
        };


    function AddAdvancedOptionsForm(containerDiv, itemID){
            var str = "<form method='post' action='#' id='addformadv-" + itemID + "'>";
            str += "Size: <input id='Size-" + itemID + "' type='text' value='1' name='Size'/><br/>";
            str += "Date: <input id='Date-" + itemID + "' type='text' value='" + GetTodaysDate() + "' name='Date'/><br/>";
            str += "Note: <input type='textarea' id='Note-" + itemID + "' name='Note' cols='20' name='Note'/><br/>";
            str += "<input id='ItemID-" + itemID + "' type='hidden' value='29' name='ItemID'/>";
            str += "<input type='submit' value='Log Item'/>";
            str += "</form>";
            str += "<button  class='cancelButton' >Cancel</button>";

            containerDiv.html(str);
        };
<div class="itemcontainer" name="item1">
<!-- <table> code -->
<button style="float:right" class="opencontainer">slide it</button>
<!-- other <table>  code -->
            <input type="hidden" value="2" name="ItemID"/>
</div>


推荐答案

尝试阻止超链接的默认操作发生:

Try preventing the default action of your hyperlink from happening:

 $(".quickaddadvoptions").click(function(e){
                e.preventDefault();
                var container = GetParentDiv($(this));
                $(container).slideToggle(function(){
                        var innerContainer = GetParentDiv(container).find("div.advancedpopoutcontainer").slideToggle(250);

                });
        });

这会阻止浏览器在< a href ='#''你动态加载的div。换句话说,锚点的行为更像是按钮而不是超链接。

That will prevent the browser from jumping when it sees the anchor '#' in <a href='#'' of your dynamically loaded div. In other words, the anchor will behave more like a button than a hyperlink.

请参阅

另请注意,返回false; 在该事件处理程序结束时也应该这样做。

Also note, a return false; at the end of that event handler should do the trick too.

这篇关于如何防止窗口在jQuery slideToggle()上向上滚动?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-12 05:19