JavaScript 학습 코드 정리 (1)
31175 단어 JavaScript
/**
* Created by wyl on 15-1-23.
*/
function displayDate()
{
document.getElementById("demo").innerHTML=Date();
}
alert("hello world") // HTML hello world
function MyFunction()
{
document.getElementById("demo").innerHTML = " id , id ";
}
document.write ("hello \
worle");
//JavaScript
var cars = new Array("baoshijie", "lanbo", "daben");
// JavaScript
//var person={
//firstname : "John",
//lastname : "Doe",
//id : 5566
//};
function GetInfo()
{
var person = {
name:"luoting",
age:20,
height:170
}
document.getElementById("demo").innerHTML=person.name+"The age is:"+person.age+"years old!"
}
// js
function getInfo()
{
var name;
var age;
var car = {
car_name:"baoshijie",
tar_amount:4,
car_price:1000000
}
name = "wyl";
age = 23;
document.getElementById("demo").innerHTML="Name:"+name+"<br>"+"age:"+age+"<br>"+car["car_name"]
+"<br>"+car.car_price+"<br>"+car.tar_amount;
}
//
document.getElementById("name").innerHTML=Date()
// ( this.innerHTML):
this.innerHTML = Date()
function displayDateTime()
{
document.getElementById("time").innerHTML = Date();
}
//html
//<button click = displayDateTime()>Now is </button>
var carname1="Volvo XC60";
var carname2='Volvo XC60';
var answer1="It's alright";
var answer2="He is called 'Johnny'";
var answer3='He is called "Johnny"';
document.write(carname1 + "<br>")
document.write(carname2 + "<br>")
document.write(answer1 + "<br>")
document.write(answer2 + "<br>")
document.write(answer3 + "<br>")
//
var txt = "Hello world"
document.write("<p>"+txt.length+"</p>")
var txt = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
document.write("<p>"+txt.length+"</p>")
//JS “===”
//!==
function test()
{
var x = 5
var y = "2"
document.getElementById("demp").innerHTML= x==='5';
document.getElementById("demo").innerHTML = y !=="4";
}
function CheckInfo(age)
{
if (age < 18)
{
document.write("too yang!");
}
else
{
document.write("Yes");
}
}
function Check()
{
var age,get_age;
age = document.getElementById("demo").value;
get_age = (age<18)?" ":" ";
document.getElementById("hehe").innerHTML = get_age;
}
// JavaScript Switch
function Compare()
{
var x;
var day = new Date().getDay();
switch (day)
{
case 0:
x = "Today is Sunday!";
break;
case 1:
x = "Today is Monday!";
break;
case 2:
x = "Today is Tuesday!";
break;
case 3:
x = "Today is Wedenday!";
break;
case 4:
x = "Today is Thrsday!";
break;
case 5:
x = "Today is Friday";
break;
case 6:
x = "Today is Saturday!";
break;
default :
x = "Looking forward to weenkend!";
}
document.getElementById("demo").innerHTML=x;
}
// JavaScript for
function ForDemo()
{
var cars = ["dazhong","baoshijie","kaidilake","QQcar"];
for (var i= 0, X=cars.length;i<X;i++)
{
document.write("car["+i+"]"+"="+cars[i]+"<br>");
}
// For/in
for (var car in cars)
{
document.write("carName:"+car);
}
}
// JavaScript While
function WhileDemo()
{
var i;
var x = 100;
var sum = "";
while(x<0)
{
sum+=i;
x--;
}
document.getElementById("demo").innerHTML = sum;
}
// JavaScript Do/While
function Do_WhileDemo()
{
var i;
var x = 100;
var sum = 0;
do
{
sum += i;
x--;
}while(x<0)
document.getElementById("demo").innerHTML=sum;
}
//Break ;
//continue
function BreakTest()
{
var x = "";
for (var i = 0;i<100;i++)
{
if(i == 20)
{
break;
}
x = x +"The number is"+x+"<br>";
}
document.getElementById("demo").innerHTML=x;
}
//continue
function ContinueDemo()
{
var x = ""
var i =0;
for(var i = 0;i<100;i++)
{
if(i==20)
{
continue;
}
x = x + i;
}
}
//JS
function LabelDemo()
{
cars=["BMW","Volvo","Saab","Ford"];
list:
{
document.write(cars[0] + "<br>");
document.write(cars[1] + "<br>");
document.write(cars[2] + "<br>");
break list;//
document.write(cars[3] + "<br>");
document.write(cars[4] + "<br>");
document.write(cars[5] + "<br>");
}
}
// typeof 。
function Typedef()
{
document.getElementById("demo").innerHTML =
typeof "john" + "<br>" +
typeof 3.14 + "<br>" +
typeof false + "<br>" +
typeof [1,2,3,4] + "<br>" +
typeof {name:'john', age:34};
}
// undefined
//eg person = undefined
//JavaScript
// JavaScript 5 :
//
//string
//number
//boolean
//object
//function
//3 :
//
//Object
//Date
//Array
//2 :
//
//null
//undefined
function isArray(myArray) {
return myArray.constructor.toString().indexOf("Array") > -1;
}
//Date()
function getDate()
{
get_date = new Date();
// Date (1 ~ 31)。
date1 = get_date.getDate();
//getDay() Date (0 ~ 6)。
day2 = get_date.getDay();
//getFullYear() Date 。
day3 = get_date.getFullYear();
//getHours() Date (0 ~ 23)。
day4 = get_date.getHours();
//getMilliseconds() Date (0 ~ 999)。
day5 = get_date.getMilliseconds();
//getMinutes() Date (0 ~ 59)。
day6 = get_date.getMinutes();
//getMonth() Date (0 ~ 11)。
day7 = get_date.getMonth();
//getSeconds() Date (0 ~ 59)。
day8 = get_date.getSeconds();
//getTime() 1970 1 1 。
day9 = get_date.getTime()
}
function myFunction()
{
var str = document.getElementById("demo").innerHTML;
var txt = str.replace(/microsoft/i,"W3cSchool");
document.getElementById("demo").innerHTML = txt;
}
//JS
function try_catch()
{
var txt = "";
try{
alaaaert("fuck you!");//
}
catch(err){
txt=" 。
";
txt+=" :" + err.message + "
";
txt+=" 。
";
alert(txt);
}
}
//hrow
function myFunction()
{
try
{
var x=document.getElementById("demo").value;
if(x=="") throw "empty";
if(isNaN(x)) throw "not a number";
if(x>10) throw "too high";
if(x<5) throw "too low";
}
catch(err)
{
var y=document.getElementById("mess");
y.innerHTML="Error: " + err + ".";
}
}
//JavaScript
// 。
// , , 。
// , 。
// , ( ), 。
// F12 , "Console" 。
//console.log()
// , console.log() JavaScript :
function Test()
{
a = 5;
b = 6;
c = a + b;
console.log(c);
}
//debugger
//debugger JavaScript, 。
// 。
// ,debugger 。
// debugger , 。
function debuger()
{
var x = 5*15;
debuger();
document.getElementById("demo").innerHTML = x;
}
// ( ) 。 , , false, true( ):
function validateForm()
{
var x = document.forms["myForm"]["fname"].value;
if(x==null || x =="")
{
alert("First Name must be filled out!");
return false;
}
}
// form :
//<form name="myForm" action="demo_form.asp" onsubmit="return validateForm()" method="post">
//First name: <input type="text" name="fname">
//<input type="submit" value="Submit">
//</form>
//E-mail
//
// , @ (.)。 ,@ , @ :
function test_email()
{
var x = document.forms["MyForm"]["email"].value;
var adops = x.indexOf("@");
var dotpos = x.lastIndexOf(".");
if(atpos<1 || dotpos<atpos+2 || dotpos+2 >= x.length)
{
alert("Not a Valid e-mail address");
return false;
}
}
/*
<form name="myForm" action="demo_form.asp" onsubmit="return validateForm();" method="post">
Email: <input type="text" name="email">
<input type="submit" value="Submit">
</form>
*/
//JS JSON
function js_json()
{
var text = '{"employees":[' +
'{"firstName":"John","lastName":"Doe" },' +
'{"firstName":"Anna","lastName":"Smith" },' +
'{"firstName":"Peter","lastName":"Jones" }]}';
obj = JSON.parse(text);
document.getElementById("demo").innerHTML =
obj.employees[1].firstName + " " + obj.employees[1].lastName;
}
/*
javascript:void(0)
javascript:void(0) , JavaScript javascript:void(0) ?
javascript:void(0) void , void JavaScript , 。
*/
// , 。
//<a href="javascript:void(0)"> </a>
/*
,void(0) 0, Javascript 。
, :
<p> :</p>
<a href="javascript:void(alert('Warning!!!'))"> !</a>
*/
/*
a undefined :
<head>
<script type="text/javascript">
<!--
function getValue(){
var a,b,c;
a = void ( b = 5, c = 7 );
document.write('a = ' + a + ' b = ' + b +' c = ' + c );
}
//-->
</script>
</head>
*/
/*
href="#" href="javascript:void(0)"
# , #top 。
javascript:void(0), 。
# , :# + id。
javascript:void(0) 。
*/
/*
<html>
<head>
<script type="text/javascript">
<!--
//-->
</script>
</head>
<body>
<p> :</p>
<a href="javascript:void(0);"> !</a>
<br>
<a href="#pos"> !</a>
<br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br>
<p id="pos"> </p>
</body>
</html>
*/
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
기초 정리 - 1문자 (String) 숫자 (Number) 불린 (Boolean) null undefined 심볼 (Symbol) 큰정수 (BigInt) 따옴표로 묶어 있어야 함 Not-A-Number - 숫자 데이터 / 숫자로 표...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.