如何将html字符串作为节点插入父元素

比如有这样的一个html字符串

'<div style="width:12px; height:12px">text</div>'

要插入到

<div class="parent">
    <div>child</div></div>

结果要是这样

<div class="parent">
    <div style="width:12px; height:12px">text</div>
    <div>child</div></div>


潇潇雨雨
浏览 990回答 1
1回答

收到一只叮咚

&nbsp; <div class="parent">&nbsp; &nbsp; <div>child</div>&nbsp; </div>&nbsp; <script type="text/javascript">&nbsp; var s = '<div style="width:12px; height:12px">text</div>';&nbsp; document.querySelector('.parent').insertAdjacentHTML('afterBegin',s);&nbsp; </script>&nbsp; <div class="divnode">&nbsp; &nbsp; <div>child</div>&nbsp; </div>&nbsp; <script type="text/javascript">&nbsp; var s = '<div style="width:12px; height:12px">text</div>';&nbsp; document.querySelector('.divnode').innerHTML = s + document.querySelector('.divnode').innerHTML;&nbsp; </script>
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript