[英]Why am i getting javascript and html code when using ajax to send and return value from php?
[英]Why am I getting Null when I want to get a String from an HTML with JavaScript
我正在嘗試制作一個紀念森的時鍾,我想制作它以便人們可以在其中插入某個日期。
但是當我在 JS 中通過 ID 獲取元素時,我得到了 null。
試圖讓它成為一個字符串並用警報測試它,但我沒有得到任何結果。 它仍然是空的。
我的意思是,警報框是空的,所以我認為它是空的。
我的一些假設是:
這是代碼:
HTML
<!DOCTYPE html>
<html lang="en" dir="ltr">
<head>
<meta charset="utf-8">
<title>💀 Days Until Death</title>
<link rel="stylesheet" href="styles.css">
</head>
<body>
<div class="main-div">
<h2>💀 Days Until Death</h2>
<hr>
<h1><p id="demo"></p></h1>
<hr>
<input id="targetDate" type="input" name="date" value="Jan 1, 2090 15:37:25">
<input onclick="ChangeDate()" type="submit" name="" value="Change Date">
</div>
</body>
<script type="text/javascript" src="script.js"></script>
</html>
JavaScript:
// Set the date we're counting down to
var countDownDate = new Date("Jan 5, 2090 15:37:25").getTime();
function ChangeDate() {
var targetDate = String(document.getElementById("targetDate").innerHTML);
alert(targetDate);
countDownDate = new Date(targetDate).getTime();
}
// Update the count down every 1 second
var x = setInterval(function() {
// Get today's date and time
var now = new Date().getTime();
// Find the distance between now and the count down date
var distance = countDownDate - now;
// Time calculations for days, hours, minutes and seconds
var days = Math.floor(distance / (1000 * 60 * 60 * 24));
var hours = Math.floor((distance % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60));
var minutes = Math.floor((distance % (1000 * 60 * 60)) / (1000 * 60));
var seconds = Math.floor((distance % (1000 * 60)) / 1000);
// Display the result in the element with id="demo"
document.getElementById("demo").innerHTML = days + "d " + hours + "h "
+ minutes + "m " + seconds + "s ";
// If the count down is finished, write some text
if (distance < 0) {
clearInterval(x);
document.getElementById("demo").innerHTML = "EXPIRED";
}
}, 1000);
您需要更改innerHTML
的value
也不需要將其轉換為String
,並避免使用 html 內聯事件偵聽器
// Set the date we're counting down to let countDownDate = new Date("Jan 5, 2090 15:37:25").getTime(); const demo = document.getElementById('demo'); const changeDateInput = document.getElementById('changeDateInput'); function ChangeDate() { const targetDate = document.getElementById('targetDate').value; console.log(targetDate); countDownDate = new Date(targetDate).getTime(); } changeDateInput.addEventListener('click', ChangeDate) // Update the count down every 1 second const x = setInterval(() => { // Get today's date and time const now = new Date().getTime(); // Find the distance between now and the count down date const distance = countDownDate - now; // Time calculations for days, hours, minutes and seconds const days = Math.floor(distance / (1000 * 60 * 60 * 24)); const hours = Math.floor((distance % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60)); const minutes = Math.floor((distance % (1000 * 60 * 60)) / (1000 * 60)); const seconds = Math.floor((distance % (1000 * 60)) / 1000); // Display the result in the element with id="demo" demo.innerHTML = `${days}d ${hours}h ${minutes}m ${seconds}s `; // If the count down is finished, write some text if (distance < 0) { clearInterval(x); demo.innerHTML = "EXPIRED"; } }, 1000);
<div class="main-div"> <h2>💀 Days Until Death</h2> <hr> <h1> <p id="demo"></p> </h1> <hr> <input id="targetDate" type="input" name="date" value="Jan 1, 2090 15:37:25"> <input id="changeDateInput" type="submit" name="" value="Change Date"> </div>
嘗試改用document.querySelector
,或者您也可以只獲取方法value
而不是innerHTML
。
還可以通過不將var
用於let
和const
來了解 javascript 的良好實踐。
https://hackernoon.com/why-you-shouldnt-use-var-anymore-f109a58b9b70 https://www.geeksforgeeks.org/what-is-the-disadvantage-of-using-innerhtml-in-javascript/
let countDownDate = new Date("Jan 5, 2090 15:37:25").getTime();
function ChangeDate() {
const newTargetData = document.querySelector("#targetDate").value;
alert(newTargetData);
countDownDate = new Date(newTargetData).getTime();
}
// Update the count down every 1 second
const x = setInterval(function() {
// Find the distance between now and the count down date
const distance = countDownDate - Date.now();
// Time calculations for days, hours, minutes and seconds
const days = Math.floor(distance / (1000 * 60 * 60 * 24));
const hours = Math.floor((distance % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60));
const minutes = Math.floor((distance % (1000 * 60 * 60)) / (1000 * 60));
const seconds = Math.floor((distance % (1000 * 60)) / 1000);
// Display the result in the element with id="demo"
document.getElementById("demo").innerHTML = days + "d " + hours + "h "
+ minutes + "m " + seconds + "s ";
// If the count down is finished, write some text
if (distance < 0) {
clearInterval(x);
document.getElementById("demo").innerHTML = "EXPIRED";
}
}, 1000);
伙計,你的黑客很好!
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.