[英]Add header on top of text area via javascript
我有這個示例代碼:
window.addEventListener('load', () => { // sticky profile notes const notes = document.querySelector('#id_notes'); // Get textarea element const headerDiv = document.createElement("div"); // Create a <div> element headerDiv.innerHTML = "SHOULD BE ON TOP"; // Insert instructions // add header on top notes.prepend(headerDiv); });
<body> <textarea id="id_notes" style="background-color: #ffffcc;"> </textarea> </body>
其中我計划添加一個與 textarea 大小相同的標題。 但不是將其添加到 textarea 頂部,而是將其添加到其中。
我只能使用 JS,因為我不知道如何將它添加到 Django 模板上。 提前致謝!
編輯:添加所需的最終結果:
您可以使用Node#insertBefore
:
window.addEventListener('load', () => { // sticky profile notes const notes = document.querySelector('#id_notes'); // Get textarea element const headerDiv = document.createElement("div"); // Create a <div> element headerDiv.innerHTML = "SHOULD BE ON TOP"; // Insert instructions // add header on top notes.parentNode.insertBefore(headerDiv, notes); });
<textarea id="id_notes" style="background-color: #ffffcc;"></textarea>
使用insertBefore()代替
window.addEventListener('load', () => { const notes = document.querySelector('#id_notes'); // Get textarea element const headerDiv = document.createElement("div"); // Create a <div> element headerDiv.innerHTML = "SHOULD BE ON TOP"; // Insert instructions // add header on top notes.parentNode.insertBefore(headerDiv, notes) });
<body> <textarea id="id_notes" style="background-color: #ffffcc;"> </textarea> </body>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.