How can I write a script which help me to add H1 tag in between Div tag in html?

◇◆丶佛笑我妖孽 提交于 2021-02-11 15:30:07

问题


I am working on Squarespace website. I am trying to add H1 tag automatically via script in Div tag. Below is the div tag in which I have text. I want to asign H1 tag to this text via script or any other method.

<div class="image-slide-title">
St. Stephens
</div>

The squarespace doesn't allow me to add H1 tag manually via coding. I can't use CSS code and when I use CSS code it changes the class and mess the area.

enter image description here


回答1:


You could try to insert it in the DOM, like this :

const myDiv = document.getElementsByClassName('image-slide-title')[0];

let title = document.createElement('h1');
title.innerHTML = 'whatever you would like it to be';

myDiv.appendChild(title);



回答2:


If in the div there is only a text:

$('.image-slide-title').html("<h1>" + $('.image-slide-title').text() + "</h1>");



回答3:


Try the following code snippet,

var  container = document.querySelector('.image-slide-title');
var title = '<h1>'+container.innerHTML+'</h1>';
container.innerHTML = title;
<div class="image-slide-title">
St. Stephens
</div>


来源:https://stackoverflow.com/questions/58621321/how-can-i-write-a-script-which-help-me-to-add-h1-tag-in-between-div-tag-in-html

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!