gpt4 book ai didi

javascript - 如何在没有 html 的情况下将 jQuery 包含在我的 js 文件中

转载 作者:行者123 更新时间:2023-12-02 21:59:34 24 4
gpt4 key购买 nike

我制作了一个 chrome 扩展,其中我的弹出按钮调用脚本。另一个脚本使用 jQuery,但我收到一条错误消息,指出 jQuery 未定义。

我的popup.html:

<!DOCTYPE html>
<html>
<head>
<title>HomAttendance</title>
</head>
<body>
<h1 style="color:#E54E4E">Hom<span style="color:#4E97E5">Attendance</span></h1>
<button type id="record" style="background-color:White"><h1 style="color:Black">Record Attendance</h1></button>
</body>
<script src="jquery-3.4.1.min.js"></script>
<script src="popup.js"></script>
</html>

我的popup.js:

document.addEventListener('DOMContentLoaded', function() {
var login = document.getElementById('record');
login.addEventListener('click', function() {
chrome.tabs.executeScript({file: 'markStudents.js'});
});
});

myScript.js:

var arrays = []
$.get('Attendance.txt', function(data){

var splitted = data.split("\n"); // --> should return an array for each line

// Loop through all lines
for(var i = 0; i < splitted.length; i++)
{

var line = splitted[i];

// Now you could remove [ and ] from string
var removed = line.replace('[','').replace(']','');
var refined = removed.replace(' ', '');

// Now you can split all values by using , delimiter
var values = refined.split(',');
var array = [];
// Now you can iterate through all values and add them to your array
for(var c = 0; c < values.length; c++)
{
var value = values[c];
array.push(value);
}
arrays.push(array);
}
});
var present = arrays[0];
console.log(present);
var absent = arrays[1];
console.log(absent);

var user = present[0];
var pass = absent[0];
var loginField = document.getElementById('fieldAccount');
var passwordField = document.getElementById('fieldPassword');
loginField.value = user;
passwordField.value = pass;
var loginForm = document.getElementById('btn-enter-sign-in');

有什么方法可以将我的 jquery.js 包含在 myScript.js 中吗?

Console Error

最佳答案

只需在导入 popup.js 之前导入 jquery

像这样

<!DOCTYPE html>
<html>
<head>
<title>HomAttendance</title>
</head>
<body>
<h1 style="color:#E54E4E">Hom<span style="color:#4E97E5">Attendance</span></h1>
<button type id="record" style="background-color:White"><h1 style="color:Black">Record Attendance</h1></button>
</body>
<script src="jquery-3.4.1.min.js"></script>
<script src="popup.js"></script>
</html>

在 popup.js 中,当您加载使用 jQuery 的 markStudents.js 时,您必须在相同之前再次加载 jQuery

像这样

document.addEventListener('DOMContentLoaded', function () {
var login = document.getElementById('record');
login.addEventListener('click', function () {
chrome.tabs.executeScript(null, { file: "jquery-3.4.1.min.js" }, function () {
chrome.tabs.executeScript(null, { file: "markStudents.js" });
});
});
});

关于javascript - 如何在没有 html 的情况下将 jQuery 包含在我的 js 文件中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59915307/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com