Tso cov ntawv zais zais ntawm cov ntawv sau txhim kho qhov pom pom ntawm lub vev xaib - nws thauj mus rau hauv browser raws nraim li tus tsim qauv tsim nws, tsis hais cov xov xwm ntau npaum li cas. Tsis tas li ntawd, nws yooj yim dua rau tus qhua - hauv kev tshawb nrhiav qhov tsim nyog thaiv ntawm cov ntaub ntawv, nws tsis tas yuav saib tag nrho cov kab ntawv, tab sis tsuas yog "cov tswv yim me me ntawm cov dej khov nab kuab".
Nws yog qhov tsim nyog
Yooj yim kev paub ntawm HTML thiab JavaScript
Cov Lus Qhia
Kauj ruam 1
Siv tus cwj pwm JavaScript kev cai los ua kom zais thiab qhia cov ntawv xav tau hauv cov nplooj ntawv HTML. Ib qho kev ua haujlwm rau txhua lub block yog qhov yooj yim dua li ntxiv cov lej rau txhua ntawm nyias. Hauv seem qhov chaw ntawm lub cim ntawm nplooj ntawv, muab cov lus qhib thiab kaw cov ntawv cim, thiab nruab nrab ntawm lawv tsim cov kev ua tsis muaj npe nrog lub npe, piv txwv li, sib pauv thiab ib qho xav tau parameter id: kev ua haujlwm sib pauv (id) {}
Kauj ruam 2
Ntxiv ob kab ntawm JavaScript code rau lub cev ntawm txoj haujlwm, ntawm cov nkhaus nkhaus. Thawj kab yuav tsum nyeem cov xwm txheej tam sim no ntawm lub thaiv ntawm cov ntawv sau - txawm hais tias nws qhov kev pom pom yog qhib lossis tawm. Muaj yuav muaj ob peb qhov thaiv nyob rau hauv ib daim ntawv, yog li txhua tus yuav tsum muaj nws tus kheej tus kheej - nws yog nws txoj haujlwm uas tau txais daim id raws li qhov kev nkag siab xwb. Siv cov cim no, nws tshawb nrhiav qhov tsim nyog thaiv hauv cov ntawv, muab qhov pom pom / pom tus nqi (lub xeev ntawm cov khoom ntiag tug) rau sDisplay kuj sib txawv: sDisplay = document.getElementById (id).style.display;
Kauj ruam 3
Qhov thib ob kab yuav tsum hloov cov khoom ntiag tug ntawm cov khoom ntawm cov ntawv xav hauv qab-nraim - zais yog tias cov ntawv nyeem pom, thiab qhia yog tias nws tau muab zais. Qhov no tuaj yeem ua tiav nrog cov cai hauv qab no: document.getElementById (id).style.display = sDisplay == 'tsis muaj'? '': 'tsis muaj';
Kauj ruam 4
Ntxiv cov ntawv sau hauv qab no rau seem seem: ib {tus ntsuas phoo: pointer} Koj yuav xav tau qhov no los ua kom pom lub pointer nas kom raug thaum koj hover dhau qhov txuas tsis ua tiav. Nrog kev pabcuam ntawm cov kev sib txuas, koj teeb tsa hauv nplooj ntawv toggle pom / pom / pom ntawm cov ntawv thaiv.
Kauj ruam 5
Muab cov toggle txuas hauv cov ntawv nyeem ua ntej txhua daim thaiv, thiab nyob rau hauv cov ntawv thaiv tom kawg ntawm cov ntawv nyeem, ntxiv ib qhov txuas zoo sib xws. Nqa cov ntawv uas pom tsis tau nyob hauv cov kab lus ncua uas muaj qhov pom tsis pom nyob hauv lawv cov cwj pwm style. Piv txwv li: nthuav tawm cov ntawv nyeem +++ Qhov no yog cov ntawv nyeem zais --- Hauv qhov ua piv txwv no, nyem rau ntawm peb lub txuas txuas ntxiv yuav hu rau txoj haujlwm saum toj no onClick kev tshwm sim, dhau nws tus ID ntawm lub block yuav tsum pom. Thiab sab hauv thaiv muaj qhov txuas ntawm peb minuses nrog tib lub luag haujlwm - nyem rau ntawm nws yuav zais cov ntawv nyeem.
Kauj Ruam 6
Tsim cov naj npawb uas yuav tsum muaj ntawm cov ntawv sau, zoo ib yam li cov uas tau piav qhia hauv qib dhau los, nco ntsoov hloov lub ID hauv qhov cim tus cwj pwm ntawm kev ntsuas ncua thiab hauv cov sib txawv dhau mus rau txoj haujlwm los ntawm onClick qhov tshwm sim hauv ob kab ntawv txuas.