You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

调整SharePoint代码:仅指定FAQ WebPart默认折叠并添加展开按钮

Solution: Target Only FAQ WebPart (WebPartWPQ7) with Default Collapse

Got it, let's tweak your script to exactly match what you need: only the FAQ WebPart gets an expand/collapse button, and it starts in a collapsed state. Other WebParts won't have any toggle functionality added. Here's the modified code and breakdown:

Modified Code

<script type="text/javascript">
// Expand/Collapse Logic for Toggle Button
function WPToggle(thisId, ImageId) {
    if (document.getElementById(thisId).style.display == "none") {
        document.getElementById(thisId).style.display = "";
        document.getElementById(ImageId).src = "/_layouts/images/minus.gif";
    } else {
        document.getElementById(thisId).style.display = "none";
        document.getElementById(ImageId).src = "/_layouts/images/plus.gif";
    }
}

// Target Only the FAQ WebPart (WebPartWPQ7)
function ExpandCollapseFAQ() {
    const targetWPId = "WebPartWPQ7";
    const targetWPTitleId = "WebPartTitleWPQ7";
    const toggleImageId = "ToggleImageFAQ";

    try {
        // Add the toggle button to the FAQ WebPart's title bar
        document.getElementById(targetWPTitleId).innerHTML = 
            `<IMG id="${toggleImageId}" onClick="WPToggle('${targetWPId}','${toggleImageId}')" 
              alt="Expand/Collapse" style="margin:6px 5px 0px 2px; float:left; cursor:pointer;" 
              src="/_layouts/images/plus.gif" />` + 
            document.getElementById(targetWPTitleId).innerHTML;
        
        // Set FAQ WebPart to be collapsed by default
        document.getElementById(targetWPId).style.display = "none";
    } catch (err) {
        console.log("FAQ WebPart not found on page:", err);
    }
}

// Run the function after SharePoint finishes loading the page
_spBodyOnLoadFunctionNames.push("ExpandCollapseFAQ");
</script>

Key Changes Made

  • Removed the global WebPart loop: Instead of iterating through every WebPart on the site, we directly target the FAQ WebPart by its specific ID (WebPartWPQ7). This ensures no other WebParts get the toggle button.
  • Default collapsed state: We explicitly hide the FAQ WebPart's content on page load with document.getElementById(targetWPId).style.display = "none", and use the plus.gif icon to signal it's ready to expand.
  • Clearer function naming: Renamed ExpandCollapseBody to ExpandCollapseFAQ so the function's purpose is immediately obvious.
  • Basic error handling: Added a console log to help troubleshoot if the FAQ WebPart isn't detected (useful if the WebPart ID ever changes or it's missing from the page).

How to Use

Just replace your existing script with this modified version. When the page loads, only the FAQ WebPart will have the expand/collapse button, and it will start hidden—users can click the plus icon to reveal its content, and click the minus icon to collapse it again later.

内容的提问来源于stack exchange,提问作者notadeveloper

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.15 08:12:26