مетод insertAdjacentText() للعنصر DOM HTML

تعريف والاستخدام

insertAdjacentText() يضيف النص في الموضع المحدد.

المواقف المسموح بها:

القيمة وصف
afterbegin بعد بدء العنصر (الشخصية الأولى).
afterend بعد العنصر.
beforebegin قبل العنصر.
beforeend قبل انتهاء العنصر (الشخصية الأخيرة).

مثال

مثال 1

إدراج النص بعد عنصر العنوان:

const h2 = document.getElementById("myH2");
let text = "My inserted text";
h2.insertAdjacentText("afterend", text);

جرب بنفسك

مثال 2

استخدام "afterbegin":

const h2 = document.getElementById("myH2");
let text = "My inserted text";
h2.insertAdjacentText("afterbegin", text);

جرب بنفسك

مثال 3

استخدام "beforebegin":

h2.insertAdjacentText("beforebegin", text);

جرب بنفسك

مثال 4

استخدام "beforeend":

h2.insertAdjacentText("beforeend", text);

جرب بنفسك

النص النهائي

element.insertAdjacentText(position, text)

أو

node.insertAdjacentText(position, text)

المتغيرات

المتغيرات وصف
position

مطلوب. موقف النص بالنسبة للعنصر:

  • afterbegin
  • afterend
  • beforebegin
  • beforeend
text النص الذي سيتم إدراجه.

دعم المتصفح

جميع المتصفحات تدعم element.insertAjacentText()

Chrome IE Edge Firefox Safari Opera
Chrome IE Edge Firefox Safari Opera
الدعم الدعم الدعم الدعم الدعم الدعم