Javascript jquery ajax$.get结果不';我找不到课

Javascript jquery ajax$.get结果不';我找不到课,javascript,jquery,ajax,history.js,Javascript,Jquery,Ajax,History.js,我被一个看似简单的问题困住了 我想从一个ajax请求中获取得到的HTML,看看一个特定的元素是否有一个特定的类,然后获取响应中另一个元素的HTML,并用它来替换某些东西等等 $(function() { $('.js-nav').on('click', function(e) { e.preventDefault(); var $this = $(this); var $url = $this.attr('href');

我被一个看似简单的问题困住了

我想从一个ajax请求中获取得到的HTML,看看一个特定的元素是否有一个特定的类,然后获取响应中另一个元素的HTML,并用它来替换某些东西等等

$(function() {
    $('.js-nav').on('click', function(e) {
        e.preventDefault();
        var $this = $(this);
        var $url = $this.attr('href');

        $.get($url.url, function(response) {
                var $response = $(response);

                var $body = $response.find('.js-wrapper').html();
                var _isLight = $response.find('.js-wrapper').hasClass('body-light');

                console.log($body);
                console.log(_isLight);

                History.pushState({ state:$url }, $title, $url);
        });
    });
});
控制台返回以下内容:

undefined
false
现在,
.js wrapper
元素不在标题中,而是在主体内部的
div

响应HTML(console.logged在使用$.get检索后退出)如下所示:

<!doctype html>
<!--[if lt IE 7]><html class="no-js ie6 oldie" lang="en"><![endif]-->
<!--[if IE 7]><html class="no-js ie7 oldie" lang="en"><![endif]-->
<!--[if IE 8]><html class="no-js ie8 oldie" lang="en"><![endif]-->
<!--[if gt IE 8]><!--><html class="no-js" lang="en"><!--<![endif]-->
<head>
    <meta charset="utf-8">

    <title>
        some title ...
    </title>

    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <!-- some header stuff like css files and scripts(the ones that HAVE to be in the header) -->

</head>
<body>



    <div class="js-wrapper wrapper body-light">
        <header class="js-header header">
            <!-- some content divs and whatnot -->

            <nav class="header-nav">
                <ul class="js-header-nav-list header-nav-list">

                        <li class="header-nav-item">
                            <!-- a couple nav li -->
                        </li>

                </ul>
            </nav>
        </header>

                <div class="content">
                    <!-- Content -->
                </div>

        <footer class="footer">
            <section class="footer-newsletter">

                    <!-- footer sections... -->

            </section>
        </footer>
    </div>


    <!-- Some scripts -->
</body>
</html>

一些标题。。。
(我加入了historyJS调用,以防与之相关)


一如既往,我们非常感谢您的帮助。

我会首先回复您的回复,并确保您得到正确的html

然后,您可以确认您拥有特定类的html

.hasClass正是您想要的,未定义的可能是因为您没有返回html,以确认控制台记录它

使用$('Your Div').contents()

获取html内容的步骤

然后$('whereyouwantoplacehtmldiv').append(您的Div)

似乎在这种情况下起作用

{ http://jsfiddle.net/Px6tu/ }
请尝试将响应记录到控制台日志中,以确保您得到了预期的结果


还可以查看一下

看起来,您正在尝试获取一个不应该存在的属性,即

$url.url 
您的$.get代码块应该如下所示

   $.get($url, function(response) {
                var $response = $(response);

                var $body = $response.find('.js-wrapper').html();
                var _isLight = $response.find('.js-wrapper').hasClass('body-light');

                console.log($body);
                console.log(_isLight);

                History.pushState({ state:$url }, $title, $url);
        });

感谢@Girish Sakhare评论和@Alexanders回复,我得到了解决方案:

var $response = $("<div/>").html(response);
var$response=$(“”).html(response);
现在函数看起来是这样的:

$(function() {
    $('.js-nav').on('click', function(e) {
        e.preventDefault();
        var $this = $(this);
        var $url = $this.attr('href');

        $.get($url, function(response) {
                var $response = $("<div/>").html(response);

                var $body = $response.find('.js-wrapper').html();
                var _isLight = $response.find('.js-wrapper').hasClass('body-light');

                console.log($body);
                console.log(_isLight);

                History.pushState({ state:$url }, $title, $url);
        });
    });
});
$(函数(){
$('.js nav')。在('click',函数(e)上{
e、 预防默认值();
var$this=$(this);
var$url=$this.attr('href');
$.get($url,函数(响应){
var$response=$(“”).html(response);
var$body=$response.find('.js wrapper').html();
var_isLight=$response.find('.js wrapper').hasClass('body-light');
console.log($body);
控制台日志(_isLight);
pushState({state:$url},$title,$url);
});
});
});
因此,我必须首先将响应附加到一个空的
div


谢谢

发布您的回复html请允许您发布相关的html?console.log回复并粘贴到此处。请将其添加到问题中。请参阅我已为我的问题添加了更多信息。回答是正确的,答案中有一个精简版本。是的,很奇怪!这是一个错误/输入错误,但无论如何都有效。。。谢谢你的提示。(这并没有解决问题……请参阅解决方案)控制台日志在答案中