我知道我们可以使用hasClass()
,但就我而言,我的ajax返回的是html。
我可以附加然后检查该类是否存在,但会有一个轻弹。在将 html 渲染到某处之前是否可以检查 html 中的内容?
$.ajax({
url:'some_url',
success: function(html) {
// I can do this:
$('body').append(html);
if ($('my_selector').length > 0) {
// but I don't want to do this because I don't want that the html will show to the user
}
}
});
最佳答案
是的,有可能
var yourReturnedHtml = getHtml();
return $(yourReturnedHtml).find('.someClass').length > 0;
关于javascript - 检查 ajax 调用的 html 响应中是否存在类或 id,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34152682/