简体   繁体   English

将 div 的子级推送到 javascript 数组

[英]push children of div to javascript array

I have a div with some child elements inside it.我有一个div ,里面有一些子元素。 I'm trying to loop through the div and push each child to an object array but I can't figure out how to loop through a div .我正在尝试遍历div并将每个孩子推送到 object 数组,但我不知道如何遍历div

I have tried我努力了

$("#id").children('div').each(function(){arrayName.push(this.html)}

with no luck.没有运气。 Here is what I have so far.这是我到目前为止所拥有的。

$("#todocontentInner").children('div').each(function() { 
   oldContent.push(this.html());
            });

I expect oldcontent to equal something kinda like this我希望 oldcontent 等于这样的东西

["<div class="contentitem">Bob</div>", "<div class="contentitem">Joe</div"]

What you are doing is correct, except that this is an element instance.您所做的是正确的,只是this是一个元素实例。 So in order to call the html() you need to make this a jQuery instance by $(this) .因此,为了调用html()您需要通过$(this)将其设为 jQuery 实例。 Try the code below.试试下面的代码。

 let arr = []; $("#parent div").each(function() { arr.push($(this).html()); }); console.log(arr);
 <script src="https://code.jquery.com/jquery-3.4.1.min.js" integrity="sha256-CSXorXvZcTkaix6Yvo6HppcZGetbYMGWSFlBw8HfCJo=" crossorigin="anonymous"></script> <div id="parent"> <div>child 1</div> <div>child 2</div> </div>

You dont need jQuery for this:为此,您不需要 jQuery:

 const arr = [...document.querySelectorAll('#someID > div')].map(el => el.innerHTML); console.log(arr);
 <div id="someID"> <div>1</div> <div>2</div> <div>3</div> <div>4</div> <div>5</div> <div>6</div> </div>

Your code of你的代码

$("#id").children('div').each(function(){arrayName.push(this.html)}

is almost correct.几乎是正确的。 The problem is that this.html is mistaken.问题是this.html弄错了。 You could use this.innerHTML , like你可以使用this.innerHTML ,比如

$("#id").children('div').each(function(){arrayName.push(this.innerHTML)}

because this in that context represents the element.因为在那个上下文中this代表元素。 However, you can do this in the jQuery way as well:但是,您也可以通过 jQuery 方式执行此操作:

$("#id").children('div').each(function(){arrayName.push($(this).html)}

Just to clear the air: this returns a native DOM element, whereas $(this) will return its equivalent jQuery element.澄清一下: this返回一个本地 DOM 元素,而$(this)将返回其等效的 jQuery 元素。 As .html() is a jQuery method, it will only work on the jQuery element, not the native DOM element.由于.html()是一种 jQuery 方法,因此它仅适用于 jQuery 元素,而不适用于本机 DOM 元素。 So to get the HTML of an element, you will have to use either this.innerHTML or $(this).html() .因此,要获取元素的 HTML,您必须使用this.innerHTML$(this).html()

However;然而; it will still not produce the result you expect.它仍然不会产生您期望的结果。 To push the full HTML of the child elements, you will need to push their outerHTML instead.要推送子元素的完整 HTML,您需要推送它们的outerHTML

TLDR; TLDR; You will get the expected result by pushing the HTML of the native DOM element using this.outerHTML :您将通过使用 this.outerHTML 推送本机 DOM 元素的this.outerHTML来获得预期的结果:

$("#id").children('div').each(function(){arrayName.push(this.outerHTML)})

Or if you really want to use jQuery, you will get the same result from $(this).prop('outerHTML') :或者如果你真的想使用 jQuery,你会从$(this).prop('outerHTML')得到相同的结果:

$("#id").children('div').each(function(){arrayName.push($(this).prop('outerHTML'))})

You can see a working sample here:您可以在此处查看工作示例:

 let arrayName = [] $("#id").children('div').each(function(){arrayName.push(this.outerHTML)}) console.log("First approach: ", arrayName) let arrayName2 = [] $("#id").children('div').each(function(){arrayName2.push($(this).prop('outerHTML'))}) console.log("Second approach: ", arrayName2)
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <div id="id"> <div class="contentitem">Bob</div> <div class="contentitem">Joe</div> </div>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM