如何在提示上使用用户输入在维基上搜索某些内容
问题描述:
我正在尝试使用户输入在维基或任何网站上进行搜索。如何在提示上使用用户输入在维基上搜索某些内容
var input = prompt();
if(input === "") {
window.location.href = ("https://en.wikipedia.org/wiki/Steve_Jobs");
};
想法?
答
// 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);
}
答
你可以把用户的输入,并换出用下划线空格,然后就打它的查询的末尾:
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);
};
答
我想,如果你想查询维基百科,你可以添加你的搜索词作为查询字符串参数维基百科的搜索URL,如下图所示:
// 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(' ','+');
}
答
要搜索一个Wiki或其他网站,你需要熟悉网站的URL结构。例如,您可以使用格式“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);
};