我正在努力使其在wiki或任何网站上搜索用户输入。
var input = prompt();
if(input === "") {
window.location.href = ("https://en.wikipedia.org/wiki/Steve_Jobs");
};
思想?
答案 0 :(得分:2)
您可以获取用户的输入并换出任何带下划线的空格,然后在查询末尾拍打它:
var input = prompt();
// Replace any spaces with underscores and remove any trailing spaces
input = input.trim().split(' ').join('_');
// If the user gave some input, let's search
if(input.length) {
window.location.href = ("https://en.wikipedia.org/wiki/" + input);
};
答案 1 :(得分:1)
我想如果您想搜索维基百科,可以将搜索词作为查询字符串参数附加到维基百科的搜索网址,如下所示:
// Prompt the user for something to search Wikipedia for
var input = prompt();
// If you actually have something, then search for it
if(input.trim().length > 0){
// Replace any spaces with + characters and search
window.location.href = 'https://en.wikipedia.org/w/index.php?search=' + input.replace(' ','+');
}
工作代码段
var input = prompt('What do you want to search Wikipedia for?');
if(input.trim().length > 0){
/// Replace any spaces with + characters and search
window.location.href = 'https://en.wikipedia.org/w/index.php?search=' + input.replace(' ','+');
}
答案 2 :(得分:1)
要搜索Wiki或其他网站,您需要熟悉网站的网址结构。例如,您可以使用格式“https://en.wikipedia.org/w/index.php?search=user+input”
在维基百科上启动搜索使用与Nick Zuber类似的代码,您可以完成此任务。
var input = prompt();
// Replace any spaces with pluses
input = input.split(' ').join('+');
// If the user gave some input, let's search
if(input.length) {
window.location.href = ("https://en.wikipedia.org/w/index.php?search=" + input);
};
答案 3 :(得分:0)
// Store the user input in a variable
var input = prompt();
// If the input wasn't empty, continue
if (input !== "") {
// Send the user to the URL concatenating the input onto the end
window.location.href = ("https://en.wikipedia.org/wiki/" + input);
}