为什么在执行 javascript 函数后 Sharepoint 会自行刷新? [英] Why Sharepoint refresh it self after a javascript function was executed?

查看:69
本文介绍了为什么在执行 javascript 函数后 Sharepoint 会自行刷新?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在内容编辑器中创建了一个按钮,它会更改例如内容框的背景颜色.

I've created a Button in a Content Editor, which changes for example the background color of the content box.

按下按钮后,背景颜色从黑色变为白色.但是Sharepoint会自动刷新它并没有保存更改.有没有人对此有想法?它也发生在 IE 和 Chrome 中.

After i press the button, the background color is changed from black to white. But Sharepoint refresh it self automatically and didnt save the change. Does anyone have an idea about this ? It happens in IE and Chrome as well.

此致,

安迪

<button onclick="changeCo()">Change Color</button> 
  <style>
    #contentBox {
    background-Color:black;
    }
    </style><script>
    function changeCo(){
        var elem = document.getElementById("contentBox");
        var theCSSprop = window.getComputedStyle(elem,null).getPropertyValue("background-color");
        if (theCSSprop == 'rgb(0, 0, 0)') {
        document.getElementById('contentBox').style.backgroundColor = 'white';
        }else{
        document.getElementById('contentBox').style.backgroundColor = 'black';
        }
    }
    </script>

推荐答案

只需将 type='button' 添加到您的按钮元素即可.这应该可以防止页面重新加载.

Just add type='button' to your button element. This should prevent the page reload.

<button type='button' onclick="changeCo()">Change Color</button> 

但是 @Scott Marcus 建议的另一件事是,如果您想在页面刷新/重新加载后保留更改,那么您必须将这些更改存储到服务器/cookies/localStorage.

But One more thing as @Scott Marcus suggested, if you want to persist the changes even after page refresh/reload then you will have to store these changes to the server/cookies/localStorage.

这篇关于为什么在执行 javascript 函数后 Sharepoint 会自行刷新?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆