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

Categories

Hi,

I want to get the contents out of 2 divs on a page that looks like this:

<div id="mainDiv">some pears</div>
<div id="subDiv">some apples</div>

i tried this code:

let $data = $(data);
var $pears = $data.filter("#mainDiv");
var $apples = $data.filter("#subDiv");

$("#cont1").html($pears);
$("#cont2").html($apples);

but this is importing the containers as well which I dont need, only the contents so I tried:

var $pears = $data.filter("#mainDiv > *");

but now nothing is being imported. Why is that?

Thank you.

See Question&Answers more detail:os

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

1 Answer

There are no elements inside the ones shown so using the selector "#mainDiv > *" returns no matches. Text nodes are not considered elements

Try just getting the inner html instead

var pears = $data.filter("#mainDiv").html();
$("#cont1").html(pears);

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...