我如何将bodycontent分成两半,并在中间使用Pure Javascript插入html。

问题描述 投票:0回答:1

我已经用jQuery做了,但需要用PURE JS做。

jQuery的例子。

$(document).ready(function() {
  var bodyContent = Math.floor($('#content').children().length);
  var bodyContentDividedInTwo = Math.floor(bodyContent / 2);


  $('#content').children(':eq('+bodyContentDividedInTwodedInHalf+')').after(`widget goes here`);
});

这就是我在PURE JS的例子中被卡住的地方。

 var fullBodyContent = document.querySelector('#contentBody');
 var bodyContentSplitInHalf = fullBodyContent.childElementCount / 2;

 bodyContentSplitInHalf.insertAdjacentHTML('afterbegin', '<h1>Yeeeeeeesssss!</h1>');

然后,我得到了一个明显的错误,它不是一个函数。因为它是一个nodelist。

请帮助我,我知道这是个简单的问题。但不知道为什么,我总是很费劲的想弄明白这个问题。

javascript nodelist
1个回答
0
投票

这是你想要的吗?

const index = Math.floor(fullBodyContent.childElementCount / 2);
const bodyContentSplitInHalf = fullBodyContent.children[index];
© www.soinside.com 2019 - 2024. All rights reserved.