【问题标题】:jQuery selector on an item within an object对象内项目上的 jQuery 选择器
【发布时间】:2009-10-09 02:19:32
【问题描述】:

我正在尝试对从 ajax 调用返回的数据使用 jQuery 选择器。

这是我的代码 -

$.ajax({
  url: 'moo.html',
  success: function ( code )
  {
    var divs = $(code).filter(function(){ return $(this).is('div') });     
    console.log( divs ); // gives me back entire object
    console.log( $(divs[0]) ); // gives me the first object

    // console.log( $(divs[0]).('#bar')); // error
    // console.log( $(divs #foo #bar)); // error
    // console.log( $(divs).(#foo #bar)); // error
  }
});
</script>

moo.html -

<div id='foo'><div id='bar'>123</div></div><div id='biz'><div id='dev'>345</div></div>

那么如何在 foo div 中获取 bar div 的 html 内容(123)?

【问题讨论】:

    标签: jquery ajax filter


    【解决方案1】:
    var bar = $("#bar", $(code)).text();
    

    var bar = $("#bar", $(code)).html();
    

    选择合适的。

    你也可以这样解决这个问题:

    var bar = $(code).find("#bar").text();
    

    【讨论】:

      【解决方案2】:

      不确定我是否理解您的问题,但我相信这应该可以解决问题:$("#bar").html();

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-06-13
        • 1970-01-01
        • 2013-02-02
        • 2011-10-17
        • 2021-04-14
        • 2011-03-10
        相关资源
        最近更新 更多