HTML DOM insertAdjacentHTML () Method
مثال
أدخل عنصر <p> جديد بعد عنصر الرأس:
var h = document.getElementById("myH2");
h.insertAdjacentHTML("afterend",
"<p>My new paragraph</p>");
التعريف والاستخدام
تقوم insertAdjacentHTML()
الطريقة بإدراج نص بتنسيق HTML في موضع محدد.
قيم الموقف القانوني هي:
"afterbegin"
"afterend"
"beforebegin"
"beforeend"
دعم المتصفح
تحدد الأرقام الواردة في الجدول إصدار المتصفح الأول الذي يدعم الطريقة بالكامل.
Method | |||||
---|---|---|---|---|---|
insertAdjacentHTML() | 1 | 4 | 8 | 4 | 7 |
بناء الجملة
node.insertAdjacentHTML(position, text)
قيمه المعامل
Parameter | Type | Description |
---|---|---|
position | String | Required. A position relative to the element. Legal values: "afterbegin" - After the beginning of the element (as the first child) "afterend" - After the element "beforebegin" - Before the element "beforeend" - Before the end of the element (as the last child) |
text | String | The text you want to insert |
مزيد من الأمثلة
مثال
باستخدام قيمة "afterbegin":
h.insertAdjacentHTML("afterbegin", "<span style='color:red'>My span</span>");
مثال
باستخدام قيمة "beforebegin":
h.insertAdjacentHTML("beforebegin", "<span style='color:red'>My span</span>");
مثال
باستخدام قيمة "قبل":
h.insertAdjacentHTML("beforeend", "<span style='color:red'>My span</span>");