最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

Jqueryjavascript, filtering html object from ajax response - Stack Overflow

matteradmin12PV0评论

I have this piece of html:

<div id="1">
  <div class="text">
     Text for div 2 
  </div>
<img src="images/image1.jpg"></img>
</div>

<div id="2">
  <div class="text">
    Text in div 2
  </div>
  <img src="images/image2.jpg"></img>
</div>

Which I grab with a simple .ajax-call

var html = $.ajax({
         url: "htmlsnippet.html",
         cache: false,
         async: false,
         dataType: "html"
         }).responseText;

If I filter it with:

var htmlFiltered = $(html).filter("#1");

it works just fine, I get the whole div with id="1",
but if I use:

var htmlFiltered = $(html).filter("#1 .text");

the htmlFiltered variable is an empty object. I can't figure out what I'm doing wrong.

I have this piece of html:

<div id="1">
  <div class="text">
     Text for div 2 
  </div>
<img src="images/image1.jpg"></img>
</div>

<div id="2">
  <div class="text">
    Text in div 2
  </div>
  <img src="images/image2.jpg"></img>
</div>

Which I grab with a simple .ajax-call

var html = $.ajax({
         url: "htmlsnippet.html",
         cache: false,
         async: false,
         dataType: "html"
         }).responseText;

If I filter it with:

var htmlFiltered = $(html).filter("#1");

it works just fine, I get the whole div with id="1",
but if I use:

var htmlFiltered = $(html).filter("#1 .text");

the htmlFiltered variable is an empty object. I can't figure out what I'm doing wrong.

Share Improve this question asked May 5, 2011 at 12:55 rjmrjm 531 gold badge1 silver badge3 bronze badges 1
  • I don't think IDs starting with a number are valid – ThiefMaster Commented May 5, 2011 at 12:57
Add a comment  | 

3 Answers 3

Reset to default 13

You should store it this way:

$.ajax({
   url: "htmlsnippet.html",
   cache: false,
   async: false,
   dataType: "html",
   success: function(data){
      html = data;
   }
}

EDIT: Your way of obtaining html works, but it's not recommended.
You can't grab your last element because you're using filter instead of find, so you should have:

var htmlFiltered = $(html).find("#1 .text");

instead of

var htmlFiltered = $(html).filter("#1 .text");

Also W3C recommends not to have numeric IDs.

EDIT 2: This should work:

var htmlFiltered = $(html).filter("#1").find(".text");

Hope this helps. Cheers

If you don't need any special functionality given by the full $.ajax method, you should give $.load() a try:

The .load() method, unlike $.get(), allows us to specify a portion of the remote document to be inserted. This is achieved with a special syntax for the url parameter. If one or more space characters are included in the string, the portion of the string following the first space is assumed to be a jQuery selector that determines the content to be loaded.

$('#result').load('ajax/test.html #container');

http://api.jquery.com/load/#loading-page-fragments

This works for me :

$.get(url,function(content) {
    var content = $(content).find('div.contentWrapper').html();
    ...
}
Post a comment

comment list (0)

  1. No comments so far