jQuery的/ JavaScript的,从阿贾克斯响应过滤HTML对象 [英] Jquery/javascript, filtering html object from ajax response
问题描述
我有这块的html:
< DIV ID =1>
< DIV CLASS =文本>
文本的DIV 2
< / DIV>
< IMG SRC =图像/ image1.jpg>< / IMG>
< / DIV>
&所述;股利的id =2>
< DIV CLASS =文本>
在DIV 2文本
< / DIV>
< IMG SRC =图像/ image2.jpg>< / IMG>
< / DIV>
这我抢一个简单的阿贾克斯通话
VAR HTML = $阿贾克斯({
网址:htmlsnippet.html
缓存:假的,
异步:假的,
数据类型:HTML
})responseText的;
如果我用其过滤:
VAR htmlFiltered = $(HTML).filter(#1);
它工作得很好,我得到了整个DIV id为1,
但如果我使用:
VAR htmlFiltered = $(HTML).filter(#1的.text);
在htmlFiltered变量是一个空的对象。 我想不出什么我做错了。
您应该保存它是这样的:
$。阿贾克斯({
网址:htmlsnippet.html
缓存:假的,
异步:假的,
数据类型:HTML,
成功:功能(数据){
HTML =数据;
}
}
编辑:你获得HTML的方式工作,但不建议
。
因为你使用,你不能抢你的最后一个元素过滤器
而不是查找
,所以你应该有:
VAR htmlFiltered = $(HTML).find(#1的.text);
而不是
VAR htmlFiltered = $(HTML).filter(#1的.text);
此外W3C建议不要有数字ID。
编辑2 :这应该工作:
VAR htmlFiltered = $(HTML).filter(#1)找到(文本)。
希望这有助于。干杯
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.
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
这篇关于jQuery的/ JavaScript的,从阿贾克斯响应过滤HTML对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!