Why Sharepoint refresh it self after a javascript function was executed?

♀尐吖头ヾ 提交于 2019-12-13 04:12:50

问题


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

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.

Best Regards,

Andy

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

回答1:


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

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

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.



来源:https://stackoverflow.com/questions/50256800/why-sharepoint-refresh-it-self-after-a-javascript-function-was-executed

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!