1

好的,所以我已经为这个问题苦苦挣扎了几个小时......我需要修改搜索结果中的链接,以便它们在新窗口/选项卡中打开。

具体来说,它是链接到“场外”点击的搜索结果。我创建了 Item_WebPage.html 的副本,但我无法让它工作。

我想有某种异步负载把它搞砸了。

我的js代码如下:

var anchors = document.getElementsByClassName('ms-srch-item-link');
    for (var i = 0; i < anchors.length; i++) {
            anchors[i].setAttribute("target", "_blank");
    }
}

但是,“锚点”始终为“0”。我可以使用“sharepoint-document-ready-as-h*ll”功能吗?我的猜测是我的问题是在我运行代码之前并非所有内容都加载到 DOM 中......

4

3 回答 3

0

自定义显示模板是更改搜索结果行为的好方法。我使用了 JQuery 代码来确保外部链接总是在新窗口中加载:

<script type="text/javascript">

        if (window.jQuery) {
            $(window).load(function () {

                // Open external links in a new tab
                var url = '://' + window.location.hostname;
                // get the current website name, and i add :// to make sure we're looking at the right name // 
                url = url.toLowerCase(); // lowercase everything to compare apples to apples 
                $("a").each(function () {
                    var link = this; // assign the link object to another variable for easier managability 
                    var linkHref = link.href.toLowerCase(); // lower case it 
                    if (linkHref.indexOf(url) < 0 && linkHref.indexOf('javascript:') < 0) { // check to see if this A object has this domain in it and make sure it's not a javascript call 
                        link.target = '_blank'; // change the target to be in the new window 
                    } if (linkHref.indexOf('.pdf') > 0) { // check to see if this is a PDF 
                        link.target = '_blank'; // change the target to be in the new window 
                        $(link).removeAttr("onclick"); //remove the SP click event 
                    } if (linkHref.indexOf('/forms/') > 0 && linkHref.indexOf(').aspx') > 0) { //check for links in the forms library 
                        link.target = '_blank'; // change the target to be in the new window 
                        $(link).removeAttr("onclick"); //remove the SP click event 
                    }
                });


            });
        }

    </script>
于 2015-05-11T14:34:32.103 回答
0

它太长了,你可能已经解决了。

我正在寻找相同的内容并将 - target="_blank" 添加到自定义显示模板中的锚标记

于 2017-02-14T21:10:19.637 回答
0

在 SharePoint Online 中,这对我来说非常有效,但适用于所有结果。

使用 SharePoint Designer,从 _catalogs/masterpage/display templates/search 中更新 Item_CommonItem_Body.html 显示模板。

搜索以下行: var titleHtml = String.format('<a clicktype="{0}" id="{1}" href="{2}" class="ms-srch-item-link" title="{3}" onfocus="{4}" {5} >{6}</a>',

并将其替换为: var titleHtml = String.format('<a clicktype="{0}" id="{1}" href="{2}" class="ms-srch-item-link" title="{3}" onfocus="{4}" {5} target="_blank">{6}</a>',

保存更改并从浏览器母版页库中发布主要版本。当您现在搜索时,结果应该在新选项卡中打开,尽管您可能需要再次加载页面缓存 (CTRL+F5)。

来自https://social.technet.microsoft.com/Forums/ie/en-US/a7db8389-3740-4ff6-ac52-645776b29a56/search-results-in-new-tabwindow-in-sharepoint-2013

于 2017-09-20T07:35:49.900 回答