简体   繁体   English

带有时间的html / javascript onclick函数

[英]html / javascript onclick function with time

I would like that when clicking on my label I would execute a javascript function that would return the exact time of that click, with hours, minutes and seconds. 我希望在单击标签时执行一个javascript函数,该函数将返回单击的确切时间,包括小时,分钟和秒。 I have a javascript function that does what I want, but when I click on the label, nothing appears to me. 我有一个可以执行我想要的功能的javascript函数,但是当我单击标签时,什么都没有出现。 Am I doing something wrong? 难道我做错了什么?

 function getTime() { const timeNow = new Date(); const hours = timeNow.getHours(); const minutes = timeNow.getMinutes(); const seconds = timeNow.getSeconds(); let timeString = '' + ((hours > 24) ? hours - 12 : hours); timeString += ((minutes < 10) ? ":0" : ":") + minutes; timeString += ((seconds < 10) ? ":0" : ":") + seconds; timeString += (hours >= 12) ? "" : ""; return timeString; } const hoursSpan = document.getElementById('hours'); hoursSpan.textContent = getTime(); 
 <div class="wrap-input100 rs1-wrap-input100 validate-input"> <span class="label-input100">Date</span> <input class="input100" onclick="getTime()" id="hours" name="Date" placeholder="Date"> <span class="focus-input100"></span> </div> 

<input> elements donot have textContent you should change value .Set your onclick event to another function which would change value on each click. <input>元素没有textContent ,应该更改valueonclick事件设置为另一个function ,该function将在每次单击时更改value

You can also add the following line in getTime() to get rid of other changeTime() 您还可以在getTime()添加以下行以摆脱其他changeTime()

hoursSpan.textContent = getTime();

 const hoursSpan = document.getElementById('hours'); function getTime() { const timeNow = new Date(); const hours = timeNow.getHours(); const minutes = timeNow.getMinutes(); const seconds = timeNow.getSeconds(); let timeString = '' + ((hours > 24) ? hours - 12 : hours); timeString += ((minutes < 10) ? ":0" : ":") + minutes; timeString += ((seconds < 10) ? ":0" : ":") + seconds; timeString += (hours >= 12) ? "" : ""; return timeString; } function changeTime(){ hoursSpan.value = getTime(); } 
 <div class="wrap-input100 rs1-wrap-input100 validate-input"> <span class="label-input100">Date</span> <input class="input100" onclick="changeTime()" id="hours" name="Date" placeholder="Date"> <span class="focus-input100"></span> </div> 

HTML: HTML:

<div class="wrap-input100 rs1-wrap-input100 validate-input">
    <span class="label-input100">Date</span>
    <!-- this reference is passed to the called function -->
    <input class="input100" onclick="getTime(this)" id="hours" name="Date" placeholder="Date">
    <span class="focus-input100"></span>
 </div>

JavaScript: (assign timestring to value property of input field instead of textContext property) JavaScript :(将时间字符串分配给输入字段的value属性而不是textContext属性)

<script language="JavaScript">

    function getTime(self) {
        const timeNow = new Date();
        const hours = timeNow.getHours();
        const minutes = timeNow.getMinutes();
        const seconds = timeNow.getSeconds();
        let timeString = '' + ((hours > 24) ? hours - 12 : hours);
        timeString += ((minutes < 10) ? ":0" : ":") + minutes;
        timeString += ((seconds < 10) ? ":0" : ":") + seconds;
        timeString += (hours >= 12) ? "" : "";
        // Assign timeString to value property
        self.value = timeString;
    }

</script>

Just put the time displaying too inside the getTime function 只需将时间显示也放在getTime函数中

function getTime() {
     const timeNow = new Date();
     const hours = timeNow.getHours();
     const minutes = timeNow.getMinutes();
     const seconds = timeNow.getSeconds();
     let timeString = '' + ((hours > 24) ? hours - 12 : hours);
     timeString += ((minutes < 10) ? ":0" : ":") + minutes;
     timeString += ((seconds < 10) ? ":0" : ":") + seconds;
     timeString += (hours >= 12) ? "" : "";


     const hoursSpan = document.getElementById('hours');
     hoursSpan.textContent = timeString;
 }

I'd say the most likely issue here is that you're using const as a variable declarator in vanilla JS. 我想说的是,最有可能出现的问题是,您在香草JS中使用const作为变量声明器。 Your browser might not know how to properly handle it, and thus it breaks. 您的浏览器可能不知道如何正确处理它,因此它会中断。 Your code is working online here (CodePen) when it's compiled with Babel. 使用Babel编译时,您的代码正在此处(CodePen)在线工作。 Perhaps change your code to: 也许将您的代码更改为:

function getTime() {
    var timeNow = new Date();
    var hours = timeNow.getHours();
    var minutes = timeNow.getMinutes();
    var seconds = timeNow.getSeconds();
    var timeString = '' + ((hours > 24) ? hours - 12 : hours);
    timeString += ((minutes < 10) ? ":0" : ":") + minutes;
    timeString += ((seconds < 10) ? ":0" : ":") + seconds;
    timeString += (hours >= 12) ? "" : "";
    return timeString;
}

var hoursSpan = document.getElementById('hours');
hoursSpan.textContent = getTime();

Having the document.getElementById outside of getTime is not an issue. 在getTime之外放置document.getElementById并不是问题。

EDIT 编辑

I didn't realize you were setting the value of an input, simply change: 我没有意识到您正在设置输入的值,只需更改即可:

const hoursSpan = document.getElementById('hours');
hoursSpan.textContent = getTime();

To: 至:

const hoursSpan = document.getElementById('hours');
hoursSpan.value = getTime();

I'd like to add though, if you are going to use Let/Const Declarations, I'd recommend you compile your code with something like Babel so that it works across browsers. 不过,我想补充一下,如果您要使用Let / Const声明,我建议您使用Babel之类的代码来编译代码,以便它可以在浏览器中使用。

You need to use value & not textContent 您需要使用value而不是textContent

 function getTime() { const timeNow = new Date(); const hours = timeNow.getHours(); const minutes = timeNow.getMinutes(); const seconds = timeNow.getSeconds(); let timeString = '' + ((hours > 24) ? hours - 12 : hours); timeString += ((minutes < 10) ? ":0" : ":") + minutes; timeString += ((seconds < 10) ? ":0" : ":") + seconds; timeString += (hours >= 12) ? "" : ""; let hoursSpan = document.getElementById('hoursCon'); hoursSpan.value = timeString; return timeString; } 
 <div class="wrap-input100 rs1-wrap-input100 validate-input"> <span class="label-input100">Date</span> <input class="input100" onclick="getTime()" id="hoursCon" name="Date" placeholder="Date"> <span class="focus-input100"></span> </div> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM