Jquery / javascript,从ajax响应中过滤html对象

时间:2022-10-09 16:29:16

I have this piece of html:

我有这个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

我用一个简单的.ajax调用来获取

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:

它工作得很好,我得到整个div与id =“1”,但如果我使用:

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

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

htmlFiltered变量是一个空对象。我无法弄清楚我做错了什么。

3 个解决方案

#1


12  

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:

编辑:你获取HTML的方式有效,但不推荐。您无法获取最后一个元素,因为您使用的是filter而不是find,因此您应该:

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

instead of

代替

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

Also W3C recommends not to have numeric IDs.

此外,W3C建议不要使用数字ID。

EDIT 2: This should work:

编辑2:这应该工作:

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

Hope this helps. Cheers

希望这可以帮助。干杯

#2


3  

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

如果你不需要完整的$ .ajax方法给出的任何特殊功能,你应该尝试$ .load():

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.

与$ .get()不同,.load()方法允许我们指定要插入的远程文档的一部分。这是通过url参数的特殊语法实现的。如果字符串中包含一个或多个空格字符,则假定第一个空格后面的字符串部分是确定要加载的内容的jQuery选择器。

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

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

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

#3


0  

This works for me :

这对我有用:

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

#1


12  

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:

编辑:你获取HTML的方式有效,但不推荐。您无法获取最后一个元素,因为您使用的是filter而不是find,因此您应该:

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

instead of

代替

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

Also W3C recommends not to have numeric IDs.

此外,W3C建议不要使用数字ID。

EDIT 2: This should work:

编辑2:这应该工作:

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

Hope this helps. Cheers

希望这可以帮助。干杯

#2


3  

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

如果你不需要完整的$ .ajax方法给出的任何特殊功能,你应该尝试$ .load():

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.

与$ .get()不同,.load()方法允许我们指定要插入的远程文档的一部分。这是通过url参数的特殊语法实现的。如果字符串中包含一个或多个空格字符,则假定第一个空格后面的字符串部分是确定要加载的内容的jQuery选择器。

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

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

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

#3


0  

This works for me :

这对我有用:

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