【问题标题】:Why Sharepoint refresh it self after a javascript function was executed?为什么 Sharepoint 在执行 javascript 函数后会自行刷新?
【发布时间】:2018-10-19 18:34:48
【问题描述】:

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

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

最好的问候,

安迪

<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>

【问题讨论】:

  • 什么时候刷新?请记住,SharePoint 是一个基于服务器的应用程序,而客户端代码就是这样,客户端。要使客户端上的更改在页面刷新之间持续存在,您需要存储更改并重新应用它们。您可以使用 cookie 来做到这一点,但 localStorage 更容易。
  • 背景颜色发生变化,网站自行刷新。我忘了把 return false;在按钮之后:)

标签: javascript sharepoint sharepoint-designer


【解决方案1】:

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

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-01-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-04-19
    • 2019-01-22
    相关资源
    最近更新 更多