Thaum tsim cov nplooj ntawv, qee zaum tsim nyog tias thaum koj nyem lub pob muab tso rau ntawm nplooj ntawv, qee qhov xwm txheej los ntawm tus sau tshwm sim hauv qhov browser. Txhawm rau ua qhov no, koj yuav tsum tso JavaScript cov lej tso rau hauv cov ntaub ntawv tsim tawm thiab khi nws nrog khawm tsim nyog. Nyob ntawm tus lej ntawm tus lej uas yuav tsum tau ua los siv cov kev tshwm sim uas tau npaj tseg, koj tuaj yeem siv ntau txoj hauv kev los txuas lub khawm rau cov lej.
Cov Lus Qhia
Kauj ruam 1
Feem ntau, qhov JavaScript code hu raug ua txhua yam ntawm kev tshwm sim onclick, uas yog, los nyem rau ntawm sab laug nas khawm. Yog tias koj tsis xav tau ntau tus lej los piav txog qhov kev ua uas yuav tsum tau tshwm sim, tom qab ntawd txhua yam nws tuaj yeem tso ncaj qha rau ntawm lub pob khawm. Piv txwv, txhawm rau kho qhov browser kom pom cov lus yooj yooj yim thaum lub pob nyem, cov ntawv sau JavaScript yuav zoo li no: ceeb toom ('Code ua haujlwm!') Nws tsuas yog siv ib nqe lus thiab cov ntawv xwb. Tag nrho cov no tuaj yeem muab tso tau yooj yim rau hauv cov xwm txheej onclick cov lus qhia ntawm cov khawm cim. Hauv qhov no, qhov HTML code yooj yim ntawm nplooj ntawv yuav zoo li no:
Khawm nrog cov cai
Khawm nrog cov cai
Kauj ruam 2
Nws tsis yog qhov tseeb kom tso ntau cov txheej txheem JavaScript ncaj qha ncaj qha rau ntawm lub pob khawm. Nws yooj yim dua los ua ib txoj haujlwm cais tawm ntawm nws, thiab tso nws hu rau hauv kev tshwm sim onclick. Piv txwv, qhov no yuav zoo li muaj nuj nqi uas qhia pom lub qhov rai uas muaj lub sijhawm ntawm lub pob nyem: ua haujlwm getTime () {
var tam sim no = tshiab hnub ();
qhia paub ("Tus lej ua haujlwm hauv" + now.getHours () + ":" + now.getMinutes ());
} Nws yuav tsum muab tso rau hauv cov header ntawm nplooj ntawv (nruab nrab ntawm thiab lub cim npe). Txoj kev cai tag nrho ntawm nplooj ntawv nrog hu mus rau txoj haujlwm no khi rau khawm tau zoo li no:
Nuj nqi hu khawm
muaj nuj nqi getTime () {
var tam sim no = tshiab hnub ();
qhia paub ("Tus lej ua haujlwm hauv" + now.getHours () + ":" + now.getMinutes ());
}
Nuj nqi hu khawm
Kauj ruam 3
Tib txoj kev yuav tsum tau siv thaum nyem ob peb lub nyees khawm sib txawv yuav tsum tsa ib qho kev tshwm sim uas tuaj yeem piav nrog tib lub JavaScript code. Piv txwv li, koj tuaj yeem hloov kho me ntsis kev ua haujlwm yav dhau los ntxiv rau kev txheeb xyuas lub pob nyem khawm rau kab lus: ntawv muaj nuj nqi getTime (btnString) {
var tam sim no = tshiab hnub ();
ceeb toom (btnString + "nias rau hauv" + now.getHours () + ":" + now.getMinutes ());
} Tus lej tag rau ib nplooj ntawv nrog peb lub khawm zoo li no yuav zoo li no:
Peb lub khawm nrog tus nqi hu
muaj nuj nqi getTime (btnString) {
var tam sim no = tshiab hnub ();
ceeb toom (btnString + "nias rau hauv" + now.getHours () + ":" + now.getMinutes ());
}
Thawj lub pob
Thib ob khawm
Thib peb khawm