เปลี่ยนแท็ก H1 เป็น H2 โดยใช้ Javascript [ปิด]

Aug 20 2020

ฉันกำลังมองหาวิธีใน Vanilla JavaScript เพื่อเปลี่ยน<h1>แท็กเป็น<h3>แท็กสำหรับโค้ด HTML นี้:

<h1 class="price-heading ult-responsive cust-headformat" data-ultimate-target="#price-table-wrap-3686 .cust-headformat" data-responsive-json-new="{&quot;font-size&quot;:&quot;&quot;,&quot;line-height&quot;:&quot;&quot;}" style="font-family:'Roboto';font-weight:bold;">Basic</h1>

ฉันจะบรรลุเป้าหมายนี้ได้อย่างไร?

คำตอบ

1 Dalibor Aug 20 2020 at 15:50
var el = document.getElementsByTagName('h1')[0];
var dummy = document.createElement('h2');
dummy.innerHTML = el.innerHTML;
el.parentNode.replaceChild(dummy, el);

https://jsfiddle.net/Lzg47mek/