我的AJAX返回的所有HTML都包含在<div class = 'Post'>....</div>
或No new data yet
中。
var request = $.ajax({
url: "/mysite/load_latest/",
type: "POST",
data: { user_id : userID },
dataType: "html"
});
request.done(function( msg ){
console.log(msg);
var class1 = $(msg).filter(".Post");
console.log(class1);
});
我想检查返回的HTML 中是否存在.Post
,但会返回此错误:Uncaught Error: Syntax error, unrecognized expression: <div class = 'Post'>...content...</div>
我尝试$(msg).find(".Post");
,但它返回相同的错误。
答案 0 :(得分:1)
尝试创建一个新元素,将响应追加到它,然后搜索所需的元素:
request.done(function( msg ) {
var element =
// make a new element on the fly
$('<div></div>')
// put the `msg` response inside it
.html(msg)
// now search for `.Post` element
.find('.Post')
;
// the length will be `0` or more
alert(element.length);
});
答案 1 :(得分:0)
例如在html中
<div class="msg_html"></div>
代码
request.done(function( msg ){
$('.msg_html').html(msg);
$('.Post').css('background','yellow');
alert('Now your Post class has a background yellow if its a content in it');
});