更改IntialPrompt搜索框Web部件JavaScript / jQuery
-
29-09-2020 - |
题
我有一个page_layout,其中我计划被许多页面使用,这个页面布局有一个searchbox webpart,目前它有一个通用的'search ...'。然而,我想要的是改变它,所以它说'搜索你所在的任何页面'。获取我正在上的页面名称,而是对我想要的initialPrompt更改为我想要的东西,我并不是遇到麻烦。我已经尝试了像document.getElementById('NBSwebpartSiteSearch').InitialPrompt = 'test';
解决方案
...
所以!你需要的是:
//use this instead of immediate or jQuery ready because we want to manipulate SP generated markup
_spBodyOnLoadFunctionNames.push("searchPromptSetup");
function searchPromptSetup() {
function spPromptHider(searchDOMElement) {
//make sure you have a valid object
if (searchDOMElement) {
//give it a placeholder if it doesn't already have one
if (!searchDOMElement.placeholder) searchDOMElement.placeholder = "Search in whatever page you are on";
/*use SP's javascript to hide the 'Search...' prompt, note the $ here is not the jQuery alias...
the OR assignment is my lazy way of allowing the function to be used in the blur event later... */
var control = $getClientControl(searchDOMElement) || $getClientControl(this);
control.hidePrompt();
}
}
var searchBox = document.getElementById('<yoursearchboxid>');
// if you don't add this listener to the blur event it will just reset to 'Search...'
searchBox.addEventListener("blur", spPromptHider);
spPromptHider(searchBox);
}
.
上面只能使用支持占位符属性的浏览器......但是您可以扩展它以增加对那些没有的支持。