Javascript 如何通过这个获得特定的div?

Javascript 如何通过这个获得特定的div?,javascript,jquery,Javascript,Jquery,我构建了一个方法,允许用户返回单击的元素,如下所示: $('#button2').on('mouseover', function() { console.log(this); } 本申报表: <tr id="res-7" class="entry border-bottom" rel="popover" data-original-title="" title=""> <td style="padding-left: 10px"> <

我构建了一个方法,允许用户返回单击的元素,如下所示:

$('#button2').on('mouseover', function()
{
   console.log(this);
}
本申报表:

<tr id="res-7" class="entry border-bottom" rel="popover" data-original-title="" title="">
    <td style="padding-left: 10px">
        <div class="name"><strong>Test</strong></div>
        <div class="description">Foo</div>
     </td>
</tr>

测试

基本上,我的目标是获取div
name
和div
description
的内容,有人能解释一下怎么做吗?谢谢。

我肯定有更干净的方法,但这会让你得到你想要的

$('#button2').on('mouseover', function()
{
   console.log($(this).find(".name").html());
   console.log($(this).find(".description")).html());
}

由于元素上已经有
id
,因此访问
name
description
属性很容易:

$('#button2').on('mouseover', function() {
   var $res7 = $('#res-7');

   // Access the two divs
   var name = $res7.find('.name').text(); // or .html()
   var description = $res7.find('.description').text(); // or .html()

   // Print them out
   console.log(name);
   console.log(description);
});

当然,这段代码应该在jQuery
ready
事件处理程序中。

您可以使用innerHTML

$(this).find('name').innerHTML; //returns "test"
$(this).find('description').innerHTML; //returns "foo"
这将在当前元素中找到类,并返回所需的值。

类似以下内容:

不要忘记每个元素的ID必须是唯一的,因此您的代码是不正确的,因为“#button2”必须是唯一的,所以这始终是函数中的#button2


请注意text()和html()之间的区别。我使用.text()只获取文本,而不使用“强”代码。如果需要,请使用html()。

您说单击,但使用鼠标悬停。是哪一个?
$(document).on("mouseover","tr", function(){

var name = $(this).find(".name").text();
var description = $(this).find(".description").text();
 console.log("Name: "+name+"\nDecsription: "+description);

})