سؤال

I have a .js file with:

function date() 
{
    document.getElementById("id1").innerHTML = Date();
}

And a page with:

<p id="id1"></p>
<button type="button" onclick="date()">Click here</button>

But clicking on it doesn't do anything. What's missing?

هل كانت مفيدة؟

المحلول

just add

<script type='text/javascript' src="path_to_js_file"></script>

on top of your html page

نصائح أخرى

I found a problem here:

First, you need to create a Date object, like so:

var d = new Date();

Then use getDate(), getMonth(), and so on...

var full = d.getMonth + 1; // getMonth returns 0 - 11
full += "/" + d.getDate;
full += "/" + d.getFullYear;
document.getElementById("id1").innerHtml = full;

Also, don't forget to include <script src="script.js"></script> in your head.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top