使用 ChatGPT-4 編寫代碼就像讓經(jīng)驗(yàn)豐富的程序員瀏覽您的代碼一樣。
在自己的代碼中發(fā)現(xiàn)錯(cuò)誤可能很困難,在其他人編寫的代碼中更是困難。
ChatGPT 可以為您節(jié)省大量調(diào)試代碼的時(shí)間。
在使用生成式 AI 來(lái)幫助您之前,請(qǐng)查看是否可以縮小問(wèn)題范圍并收集更多信息。
弄清楚(如果可以的話):
ChatGPT 可以通過(guò)更多信息更準(zhǔn)確地找到問(wèn)題。
上一章,我們讓 ChatGPT 為我們編寫了一些網(wǎng)頁(yè)代碼?,F(xiàn)在我們?cè)陧?yè)面中添加了一個(gè)新設(shè)計(jì),代碼不再起作用:
<!DOCTYPE html>
<html>
<head>
<title>W3.CSS Template</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" rel="external nofollow" target="_blank" rel="external nofollow" target="_blank" >
<link rel="stylesheet" rel="external nofollow" target="_blank" rel="external nofollow" target="_blank" >
<style>
body,h1 {font-family: "Raleway", sans-serif}
body, html {height: 100%}
.bgimg {
background-image: url('/w3images/forestbridge.jpg');
min-height: 100%;
background-position: center;
background-size: cover;
}
</style>
</head>
<body>
<div class="bgimg w3-display-container w3-animate-opacity w3-text-white">
<div class="w3-display-middle">
<h1 class="w3-jumbo w3-animate-top">WEEKEND</h1>
<hr class="w3-border-grey" style="margin:auto;width:40%">
<p class="w3-large w3-center">35 days left</p>
</div>
<div class="w3-display-bottomleft w3-padding-large">
Powered by <a rel="external nofollow" target="_blank" rel="external nofollow" target="_blank" target="_blank">w3.css</a>
</div>
</div>
我們知道我們更改了代碼的哪一部分,如果我們按 F12(或進(jìn)入瀏覽器的開發(fā)人員模式),我們可以在“控制臺(tái)”中看到錯(cuò)誤:
Uncaught TypeError: Cannot set properties of null (setting 'innerText')
at countdownToSaturday
使用以下提示:
The countdown function of the following page no longer functions.
<!DOCTYPE html>
<html>
<head>
<title>W3.CSS Template</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" rel="external nofollow" target="_blank" rel="external nofollow" target="_blank" >
<link rel="stylesheet" rel="external nofollow" target="_blank" rel="external nofollow" target="_blank" >
<style>
body,h1 {font-family: "Raleway", sans-serif}
body, html {height: 100%}
.bgimg {
background-image: url('/w3images/forestbridge.jpg');
min-height: 100%;
background-position: center;
background-size: cover;
}
</style>
</head>
<body>
<div class="bgimg w3-display-container w3-animate-opacity w3-text-white">
<div class="w3-display-middle">
<h1 class="w3-jumbo w3-animate-top">WEEKEND</h1>
<hr class="w3-border-grey" style="margin:auto;width:40%">
<p class="w3-large w3-center">35 days left</p>
</div>
<div class="w3-display-bottomleft w3-padding-large">
Powered by <a rel="external nofollow" target="_blank" rel="external nofollow" target="_blank" target="_blank">w3.css</a>
</div>
</div>
<script>
function countdownToSaturday() {
var now = new Date();
var dayOfWeek = now.getDay(); //0-6 where 0 is Sunday, 6 is Saturday
var daysToSaturday = (dayOfWeek < 6) ? (6 - dayOfWeek) : 0; // if it's already Saturday, no days remain
var currentHour = now.getHours();
var hoursToSaturday = 0;
// If it's not Saturday or if it's Saturday but before 12:00 (noon),
// consider the remaining hours to Saturday noon
if (daysToSaturday > 0 || (daysToSaturday === 0 && currentHour < 12)) {
hoursToSaturday = (24 - currentHour + 12) % 24;
}
var days = daysToSaturday > 0 ? daysToSaturday + " day(s), " : "";
var hours = hoursToSaturday > 0 ? hoursToSaturday + " hour(s)" : "";
// if it's Saturday and past 12:00 (noon), the countdown should return "The Weekend has landed".
if (daysToSaturday === 0 && currentHour >= 12) {
document.getElementById('weekend_coundown').innerText = "The Weekend has landed";
} else {
document.getElementById('weekend_coundown').innerText = days + hours;
}
}
countdownToSaturday();
setInterval(countdownToSaturday, 1000 * 60 * 60); // update the countdown every hour
</script>
</body>
</html>
I get the following error:
Uncaught TypeError: Cannot set properties of null (setting 'innerText')
at countdownToSaturday
ChatGPT-4 的回應(yīng)可能是:
通過(guò)ChatGPT的回應(yīng),它似乎很可能會(huì)奏效。
我們不小心刪除了應(yīng)該顯示文本的元素。向新元素添加正確的 ID 應(yīng)該可以工作。
將修復(fù)程序添加到代碼并對(duì)其進(jìn)行測(cè)試:
<p id="weekend_coundown" class="w3-large w3-center">35 days left</p>
它有效!
更多建議: