Dom javascript
BekmuhammadDev
Posted on June 26, 2024
creat elemts:
const div = document.createElement("div"); //Element yaratish ya'ni shu xolatda <div></div>
const h1 = document.createElement("h1"); //Element yaratish ya'ni shu xolatda <h1></h1>
addClassList:
div.classList.add('p-5', 'bg-success'); // class qo'shish ya'ni shu xolatda
div.innerHTML = "<h1>Hello world!</h1>";//Htmlda chiqarish
AllCode:
const div = document.createElement("div"); //Element yaratish ya'ni shu xolatda <div></div>
const h1 = document.createElement("h1"); //Element yaratish ya'ni shu xolatda <h1></h1>
div.classList.add('p-5', 'bg-success'); // class qo'shish ya'ni shu xolatda
div.innerHTML = "<h1>Hello world!</h1>";//Htmlda chiqarish
console.log(h1);
console.log(div);
CreatElement Function:
function CreateElemt() {
const div = document.createElement("div");
div.classList.add('p-5', 'bg-success');
div.innerHTML = "<h1>Hello world!</h1>";
cotainer.div.append(div)
}
CreateElemt() //call qilish orqali nusxalab ya'ni ko'paytirish mumkin!
CreatElement Function For:
function CreateElemt(limit) {
for (let i = 0; i < i.limit; index++) {
const div = document.createElement("div");
div.classList.add('p-5', 'bg-success');
div.innerHTML = "<h1>Hello world!</h1>";
cotainer.div.append(div)
}
}
CreateElemt(10) //funksiyani call qilish va unga necha marotaba ko'paytirish keragligini yozish mumkin
Attribute:
getAttribute:
setAttribute
*getAttribute Metodi
*
Bu metod DOM elementining berilgan atribut qiymatini qaytaradi.
element.getAttribute(attribut_nomi)
Masalan:
let elem = document.getElementById('myElement');
let value = elem.getAttribute('class');
console.log(value); // Atributning qiymati, masalan: "myClass"
setAttribute Metodi
Bu metod DOM elementiga yangi atribut qo'shadi yoki mavjud atributning qiymatini o'zgartiradi.
element.setAttribute(attribut_nomi, yangi_ qiymat)
let elem = document.getElementById('myElement');
elem.setAttribute('class', 'newClass');
hasAttribute
JavaScriptda DOM elementining atributi mavjudligini tekshirish uchun hasAttribute metodidan foydalaniladi. Bu metod berilgan atribut elementda mavjud bo'lsa, true qiymatini, aks holda false qiymatini qaytaradi.
element.hasAttribute(attribut_nomi)
hasAttribute metodi yordamida har bir atributning mavjudligi tekshiriladi va natijalar konsolga chiqariladi.
getAttribute va setAttribute metodlari DOM manipulyatsiyasi uchun juda foydali hisoblanadi.
Posted on June 26, 2024
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.