JS Basic knowledge:
1.JavaScript: Change HTML content
Instance
X=document.getelementbyid ("demo") //Find element x.innerhtml= "Hello JavaScript"; Change Content
<script>
function MyFunction ()
{
X=document.getelementbyid ("demo"); Find element
X.innerhtml= "Hello javascript!"; Change Content
}
</script>
<button type= "button" onclick= "MyFunction ()" > click here </button>
2.JavaScript: Change HTML style
Instance
X=document.getelementbyid ("demo") //Find element x.style.color= "#ff0000"; Change Style
<script>
function MyFunction ()
{
X=document.getelementbyid ("demo")//Find element
X.style.color= "#ff0000"; Change Style
}
</script>
<button type= "button" onclick= "MyFunction ()" > click here </button>
3.JavaScript: Validate input
Instance
If IsNaN (x) {alert ("Not Numeric")};
<script>
function MyFunction ()
{
var X=document.getelementbyid ("Demo"). Value;
if (x== "" | | IsNaN (x))
{
Alert ("Not Numeric");
}
}
</script>
<button type= "button" onclick= "MyFunction ()" > click here </button>
4.JavaScript: Change HTML image
Tap the light bulb to turn the lamp on or off
<script>
function changeimage ()
{
Element=document.getelementbyid (' myimage ')
if (Element.src.match ("Bulbon"))
{
element.src= "/i/eg_bulboff.gif";
}
Else
{
element.src= "/i/eg_bulbon.gif";
}
}
</script>
/i/eg_bulboff.gif" >
5. The function is called when the button is clicked
<Script>functionmyFunction () {document.getElementById ("Demo"). InnerHTML="My First JavaScript Function";}</Script> <Buttontype= "button"onclick= "myFunction ()">Try it</Button>
JS Learning Note One