javascript - 如何使用 jquery 选择器构建具有同级标签的分层对象

标签 javascript jquery node.js jquery-selectors jsdom

我有下面的 html 片段。我想通过网络抓取页面以获取主题和子主题并将其存储在对象中。

期望的结果是:

{
'topic': 'Java Basics', 
'subtopics':['Define the scope of variables', 'Define the structure of a Java class', ...]
}

我试图让它与 Node.js 和 JQuery 的 Jsdom 一起使用:

var jsdom = require('jsdom');
var fs = require("fs");


var topicos = fs.readFileSync("topic.html", "utf-8");

    jsdom.env(topicos, ["http://code.jquery.com/jquery.js"], function (error, window) {
        var $ = window.$;
        var length = $('div ~ ').each(function () {
            //???
            var topic = $(this);
            var text = topic.text();                 
            console.log(text.trim())
        });
    })

但是由于我缺乏 jQuery 经验,我无法正确组织层次结构。

HTML 片段:

<div>
    <strong>Java Basics&nbsp;</strong></div>
<ul>
    <li>
        Define the scope of variables&nbsp;</li>
    <li>
        Define the structure of a Java class
    </li>
    <li>
        Create executable Java applications with a main method; run a Java program from the command line; including
        console output.
    </li>
    <li>
        Import other Java packages to make them accessible in your code
    </li>
    <li>
        Compare and contrast the features and components of Java such as:
        platform independence, object orientation, encapsulation, etc.
    </li>
</ul>
<div>
    <strong>Working With Java Data Types&nbsp;</strong></div>
<ul>
    <li>
        Declare and initialize variables (including casting of primitive data types)
    </li>
    <li>
        Differentiate between object reference variables and primitive variables
    </li>
    <li>
        Know how to read or write to object fields
    </li>
    <li>
        Explain an Object's Lifecycle (creation, "dereference by reassignment" and garbage collection)
    </li>
    <li>
        Develop code that uses wrapper classes such as Boolean, Double, and Integer. &nbsp;</li>
</ul>
 ...

最佳答案

这是工作片段 fiddle

var topicos = [];

jQuery('div').each(function(){
var data = {};
var jThis = jQuery(this);
  data.topic = jThis.find('strong').text();
  data.subtopics = [];
  jThis.next('ul').find('li').each(function(){
  var jThis = jQuery(this);
    data.subtopics.push(jThis.text());
  });
topicos.push(data);
});

console.log(topicos);

但我强烈建议将类添加到您的标记中,并将它们用作选择器而不是标记名称:

<div class="js-topic-data">
  <div>
    <strong class="js-topic">Java Basics&nbsp;</strong>
  </div>
  <ul>
    <li class="js-sub-topic">
       Define the scope of variables&nbsp;</li>
    <li>
  </ul>
</div>

然后你可以这样做:

jQuery('.js-topic-data').each(function(){
var data = {};
var jThis = jQuery(this);
  data.topic = jThis.find('.js-topic').text();
  data.subtopics = [];
  jThis.next('.js-sub-topic').each(function(){
  var jThis = jQuery(this);
    data.subtopics.push(jThis.text());
  });
topicos.push(data);
});

对于标记更改等来说更加稳健

关于javascript - 如何使用 jquery 选择器构建具有同级标签的分层对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36236334/

相关文章:

javascript - Next.js 上下文提供程序用页面特定布局组件包装 App 组件,提供未定义的数据

Jquery Fancybox 2 添加了右边距?

Jquery 切换表行开/关

node.js - NodeJS 有没有办法获取所有流程事件?

node.js - 如何在 express 中正确管理 CORS 策略?

javascript - Angular 绝对路线和路线/stateProvider

php - 使用 Ajax 和 PHP 进行数据输出

javascript - 将对象名称和描述定义为函数调用中的变量?

jquery - 如何使用 jQuery 为 border-top-right-radius 的水平和垂直半径设置动画

node.js - AWS Lambda 函数检查站点是否可用