我正在尝试在加载sharepoint页面后运行一些jquery代码,代码为:
$(".ms-commentcollapse-icon").click();
我在页面加载后一直使用以下内容加载代码,但它似乎不太可靠(它有时可以工作,有时也不会):
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.0/jquery.min.js"></script>
<script type="text/javascript">
$(window).load(function () {
$(".ms-commentcollapse-icon").click();
});
</script>
有没有其他好的方法来实现这一目标?我不确定发生了什么,sharepoint可能有问题,但我想我会先尝试摆弄脚本。
答案 0 :(得分:1)
您可以使用自动执行功能:
<script type="text/javascript">
(function () {
$(".ms-commentcollapse-icon").click();
} ());
</script>
答案 1 :(得分:0)
如果这是SharePoint 2010或更高版本,则可以使用ExecuteOrDelayUntilScriptLoaded(yourfunction,"SP.JS")
阻止代码触发,直到加载SP.JS库之后(或者您可以将任何其他库放在第二个参数中以获得类似的效果)。
如果这是在网络部件中,并且您不希望它在页面上的其他网页部件完全加载之前执行,请确保包含该脚本的网络部件 以下其他网页部分。
作为最后的手段,您可以使用setTimeout
或setInterval
延迟执行,但这很难看。
答案 2 :(得分:0)
您可以使用e.preventDefault()来阻止默认行为;在功能内。
<script type="text/javascript">
$(".ms-commentcollapse-icon").click(function(e) {
// We're going to stop the default behavior
e.preventDefault();
//some code here
});
</script>