对象中项目的jQuery选择器

时间:2009-10-09 02:19:33

标签: jquery ajax filter

我正在尝试对从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)?

2 个答案:

答案 0 :(得分:3)

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

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

取决于哪个。

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

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

答案 1 :(得分:0)

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