javascript - 搜索谷歌的输入
问题描述
我正在为我的 Chromebook 制作一个扩展主题,用于搜索编码网站(如这个网站、w3schools 等)。我是如何做到的?到目前为止,这是我的代码:
<html>
<head>
</head>
<body>
<input id="input1">
<button onclick="searchGoogle()">Search Google</button>
<script>
function searchGoogle() {
var one = document.getElementById("input1").value;
var two = 'http://www.google.com/search?q=' + one;
window.location = two;
}
</script>
</body>
</html>
我的代码不起作用
当它运行时,会弹出:
我的代码错了吗?
任何帮助将不胜感激。
编辑
<html>
<head>
<script src="searchgoogle.js">
</script>
</head>
<body>
<input id="input1">
<button id="link">Search Google</button>
</body>
</html>
和
document.addEventListener('DOMContentLoaded', function() {
var link = document.getElementById('link');
// onClick's logic below:
link.addEventListener('click', function() {
function searchGoogle() {
var one = document.getElementById("input1").value;
var two = 'https://www.google.com/search?q=' + one;
window.location = two;
}
});
});
也没有工作
解决方案
您在侦听器函数中声明该函数searchGoogle
,但它从未被调用。尝试:
document.addEventListener('DOMContentLoaded', function() {
var link = document.getElementById('link');
// onClick's logic below:
link.addEventListener('click', function() {
//there is no need to declare a new function here.
var one = document.getElementById("input1").value;
var two = 'https://www.google.com/search?q=' + encodeURIComponent(one);
window.location = two;
});
});
推荐阅读
- ionic3 - 如何在 Angular 6 中添加 ng2-smart-table 嵌套列
- python - 删除和移动熊猫 df 中的值
- file - 寻找在 docker 上运行的两个 Web 应用程序之间存储文件的最佳方式
- google-app-maker - AppMaker:每个部署的环境变量?获取部署名称?
- vuejs2 - vue2-google-maps 在 vue.js 中添加自定义标记
- appium-ios - 在 Bitrise for Appium iOS 测试中获取元素的可见性为 false
- abap - 如何在网格上制作表格大小,如 ABAP 中的容器大小
- c# - Xamarin Forms - 根据原始源文件大小在 C# 中设置图像大小
- linux - 如何列出加载到内核中的所有 bpf 程序?(例如 tc-bpf)
- javascript - JavaScript Array to URLencoded - obj 中的 wthis 数组