Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
233 views
in Technique[技术] by (71.8m points)

怎么用js操作这些dom元素

两个问题

怎么用js给body下面加一个id为wrapper的div,包在最外层?

怎么用js给样式为aa的div外包一个id为wrap的div?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
<div class="aa">demo</div>
<script type="text/javascript">
function fnWrapAll(s,sWrap){
    var oFrag = document.createDocumentFragment(),o = document.querySelectorAll(s),oTemp;
    o[0].insertAdjacentHTML('beforeBegin',sWrap);
    oTemp = o[0].previousSibling;
    [].forEach.call(o,function(o){
        oFrag.appendChild(o);
    });
    oTemp.appendChild(oFrag);
}
fnWrapAll('.aa','<div id="wrap"></div>');
</script>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...