我想为chrome做一个扩展,以打开有精心编制的url的页面。 该扩展只需用户复制,粘贴字符串编号和按下按钮,它将立即打开新的窗口与准备好的url+输入的字符串编号。 代码如下
我现在遇到的问题是number
总是返回null
,并且url将导致404页。 如果有人能指出为什么它返回null以及如何修复它,我将不胜感激,因为我是这个JavaScript的新手
function number() {
var number = document.querySelector("code");
chrome.windows.create({
url: "https://gamecrackzome.gg/game/" + number,
focused: true,
state: "maximized",
});
}
document.addEventListener('DOMContentLoaded', function() {
var button = document.getElementById("go");
button.onclick = number;
});
<!DOCTYPE html>
<html>
<head>
<title>Page Title</title>
<script src="popup.js"></script>
<style>
</style>
</head>
<body>
</br>
<input type="text" id="code">
</br>
<input type="submit" id="go" value="GO">
</body>
</html>
你有两个问题。
QuerySelector(“code”)
选择第一个
元素(它返回NULL
,因为您没有任何元素)。 要按ID选择元素,需要使用ID选择器:QuerySelector(“#code”)
。
value
属性:QuerySelector(“#code”).value