Javascript 获取H2标记内容的jQuery方法

Javascript 获取H2标记内容的jQuery方法,javascript,jquery,Javascript,Jquery,我有以下HTML: <div id="new_subscribed_threads" class="block"> <h2 class="blockhead">Subscribed Threads with New Posts: (0)</h2> 但我认为我的思路不对。应该是这样的: var MyVar = $(results).find("#new_subscribed_threads h2").html(); 或 但我认为我的方向不对 是的,事实上你已

我有以下HTML:

<div id="new_subscribed_threads" class="block">
<h2 class="blockhead">Subscribed Threads with New Posts: (0)</h2>

但我认为我的思路不对。

应该是这样的:

var MyVar = $(results).find("#new_subscribed_threads h2").html();

但我认为我的方向不对

是的,事实上你已经完全离开了。您不想使用类选择器
.h2
,但是


就这么简单:

  • 找到h2标签的位置
  • 通过html()访问它并获取内容
它看起来像:

var s= $('div.block >h2').html();
console.log(s);
试试这个$(results).find(“new_subscribed_threads h2”).html()
new_subscribed_threads.h2
:选择任何具有class属性且包含单词h2-
$(results)的新_subscribed_threads元素。查找(“#new_subscribed_threads h2”).html()
var MyVar = $(results).find("h2.blockhead").html();
var s= $('div.block >h2').html();
console.log(s);