简体   繁体   English

我的脚本有什么错误?

[英]What's the error in my script?

I'm having three buttons A,B,C. 我有三个按钮A,B,C。 When i click the button A the C button should hide (it's not a problem), when i click the button B the page should be refreshed ( history.go(0) ), the C button should be showed and the B button should hide, i tried by using the following code the process happened (ie) C button showed but the B button didn't hide (it's hide only for a fraction of seconds and suddenly it's showed)....... 当我单击按钮A时,C按钮应隐藏(这不是问题),当我单击按钮B时,应刷新页面(history.go(0)),应显示C按钮,而应隐藏B按钮,我尝试通过使用以下代码来完成该过程(即C按钮显示了,但是B按钮没有隐藏(它仅隐藏了几秒钟,然后突然显示了)........

<script>
$(document).ready(function() {
    $("#A").click(function() {
        $("#C").hide()
    });

    $("#B").click(function() {
        history.go(0)
        $("#C").show();
        $("#B").hide();         
    });
});

</script>

May be, due to the page refresh it will show the button B, but i'm not sure. 可能是由于页面刷新,它会显示按钮B,但我不确定。 suggest me some idea to overcome this issue. 建议我一些想法来克服这个问题。 Thanks in advance 提前致谢

Try This answer 试试这个答案

$(document).ready(function() {

    $("#C").show();

    $("#B").hide();

    $("#A").click(function() {
        $("#C").hide();
        $("#B").show();
    });

    $("#B").click(function() {
        history.go(0)

    });
});

Just set default values at start of the document 只需在文档开始处设置默认值

$(document).ready(function() {

    $("#C").show();
    $("#B").hide();         

    $("#A").click(function() {
        $("#C").hide()
    });

    $("#B").click(function() {
        history.go(0)
    });
});

history.go(0) is a page refresh and how can you expect a click action to happen on a page refresh where the page is in a new state. history.go(0)是页面刷新,您如何期望在页面处于新状态的页面刷新上发生单击动作。

Probably call the $("#C").show(); $("#B").hide(); 可能调用$("#C").show(); $("#B").hide(); $("#C").show(); $("#B").hide(); on page load and on $("#B").click just refresh the page. 在页面加载和$("#B").click只需刷新页面即可。

$(document).ready(function() {

 $("#C").show();

 $("#B").hide();


    $("#A").click(function() {
        $("#C").hide()
    });

    $("#B").click(function() {
        history.go(0)

    });
});

The problem is that you are refreshing the page and then doing the hide. 问题是您要刷新页面然后进行隐藏。 When you refresh a page, you cancel all the js execution, and therefore the code after history.go(0) does not execute at all. 刷新页面时,您将取消所有js执行,因此history.go(0)之后的代码根本不会执行。

当浏览器解释history.go()时,页面将刷新,并且脚本的其余部分将不执行。

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

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