jQuery:如何选择div中的元素?

jQuery:如何选择div中的元素?,jquery,Jquery,我有一个视频对象的列表,这些对象以HTML格式呈现为 <div class="video"> <p class='title'> {{ video.title }} </p> <p class='url'> {{ video.url }} </p> <button class="btn btn-primary queue" href="#">queue</butt

我有一个
视频对象的列表,这些对象以HTML格式呈现为

   <div class="video">
        <p class='title'> {{ video.title }} </p>
        <p class='url'> {{ video.url }} </p> 
        <button class="btn btn-primary queue" href="#">queue</button>
    </div>
    <br />
当我在firebug中看到
console.log(title)
作为firebug中的
jQuery()
时,我做错了什么?

.closest()
位于父链的上方。如果要搜索子对象,请使用
.find()
。您的代码还使它看起来像您想要的是div的内容,而不是jQuery对象。结合这些更改,您将得到以下结果:

$(function(){
    $('body').on('click', '.video', function(event) {
        var title = $(this).find('.title').html();
        console.log(title);
        alert(title);
    });
});
.closest()
位于父链的上方。如果要搜索子对象,请使用
.find()
。您的代码还使它看起来像您想要的是div的内容,而不是jQuery对象。结合这些更改,您将得到以下结果:

$(function(){
    $('body').on('click', '.video', function(event) {
        var title = $(this).find('.title').html();
        console.log(title);
        alert(title);
    });
});
.closest('.title')
将沿着DOM层次结构向上移动,直到找到与选择器匹配的元素。然后,它将返回该选择器的jQuery对象

取而代之的是遍历DOM层次结构,例如,使用方法(搜索直接子对象)或方法(搜索子对象的子对象),然后使用方法检索文本或使用方法检索HTML内容(而不是获取jQuery对象):

.closest('.title')
将沿着DOM层次结构向上移动,直到找到与选择器匹配的元素。然后,它将返回该选择器的jQuery对象

取而代之的是遍历DOM层次结构,例如,使用方法(搜索直接子对象)或方法(搜索子对象的子对象),然后使用方法检索文本或使用方法检索HTML内容(而不是获取jQuery对象):

$(function(){
  $('body').on('click', '.video', function(event) {
    var title = $(this).find('.title').html();
    console.log(title);
    alert(title);
  });
});​
var title = $(this).children('.title').text();