An warware: ts queryselectorall as htmlelement

A cikin al'ummar yau inda bayanai suka zama ruwan dare, mu'amala da takaddun HTML ba banda bane. Kowane shafin yanar gizon sararin samaniya an gina shi akan HTML, ฦ™irฦ™irar ragamar labyrinthine na abubuwa da tags. Makullin kewayawa ta wannan ragar cikin tsari da tsari shine ta hanyar amfani da na'urar tambayaSelectorAll Hanyar a JavaScript ko, a cikin yanayinmu a nan, TypeScript. A yau, za mu zurfafa zurfafa cikin yin amfani da damar tambayarSelectorAll azaman HTMLElement a cikin TypeScript.

TypeScript, babban juzu'i na JavaScript, ana iya yiwa alama alama azaman tsattsauran ra'ayi ga masu haษ“akawa da ke aiki tare da JS. Yana haษ“aka JavaScript ta hanyar ฦ™ara nau'ikan da kawo cikin tsarin Shirye-shiryen Abubuwan da ke Kan Abu. The tambayaSelectorAll Hanyar kayan aiki ne mai ฦ™arfi a cikin akwatin kayan aiki na TypeScript da kowane mai haษ“aka gidan yanar gizo.

let elements: NodeListOf<HTMLElement> = document.querySelectorAll(".class");

Zurfafa nutsewa cikin tambayaSelectorAll

Lambar lambar da ke sama, kodayake tana da kyau a gaba, tana da yadudduka a ciki. The tambayaSelectorAll Hanyar a cikin TypeScript yana aiki daidai da hanyar JavaScript na gargajiya; yana dawo da jerin abubuwan da ke cikin takaddun da suka dace da ฦ™ungiyar masu zaษ“in da aka ฦ™ayyade. Nau'in annotation na NodeListOf yana tabbatar da cewa muna hulษ—a da HTMLElements.

A cikin sauฦ™i, yana ba da damar zaษ“in kowane nau'in HTML da ke kan shafin yanar gizon da ya dace da ฦ™ayyadaddun yanayin. Misali, yana iya taimakawa wajen zaษ“ar abubuwa ta aji, id, tag, har ma da hadaddun haษ—aษ—ษ—iya.

Dakunan karatu masu alaฦ™a da Ayyuka

Duk da yake tambayaSelectorAll yana da ฦ™arfi sosai, yana da kyau a bincika manufar dangane da wasu ษ—akunan karatu, ayyuka, ko hanyoyin:

  • The samunElementById Hanyar tana ba da damar shiga kai tsaye zuwa kashi ta musamman id.
  • samunElementsByClass Name da kuma samunElementsByTagName hanyoyin tattara HTML Tarin abubuwa masu rai.
  • A cikin daular jQuery, da $('.class') sanannen zaษ“i ne don ษ—auko abubuwa ta aji.

Amfani da querySelectorAll tare da TypeScript

Hakikanin ikon tambayaSelectorAll yana haskakawa yadda ya kamata lokacin amfani da shi a cikin daular TypeScript. Godiya ga tsayayyen nau'ikan TypeScript, muna da tabbacin cewa abubuwan da aka dawo dasu za su zama HTMLElements, yin aiki na gaba akan waษ—annan abubuwan iska. Wannan yana rage girman kurakuran lokacin aiki kuma yana sauฦ™aฦ™e cikawa ta atomatik a cikin masu gyara rubutu masu tallafi.

A cikin tattara wannan faษ—uwar cikin yanayin querySelectorAll, HTMLElement da TypeScript, a bayyane yake cewa ba za a iya raina ฦ™arfin irin waษ—annan fasahohin ba. Ta hanyar haษ—a zaษ“i na hankali tare da samun damar shirye-shirye zuwa abubuwa akan shafin yanar gizon, mutum na iya ฦ™irฦ™irar ฦ™warewar mu'amala ta gaske da kuzari. Tushen ilimin waษ—annan iyawar shine, don haka, kayan aiki mai ฦ™arfi a cikin bel na kowane mai haษ“aka gidan yanar gizo na zamani.

Shafi posts:

Leave a Comment