Node. js notes (10) Nodejs and DOM operations, node. jsnodejs

Source: Internet
Author: User

Node. js notes (10) Nodejs and DOM operations, node. jsnodejs

As a back-end language, Nodejs directly performs DOM operations against the separation principle.
However, if it is a small personal website, it is no harm if it is a little more flexible.
Here we will introduce the cheerio plug-in, which is implemented based on jquery and slightly streamlined to provide nodejs with dom operations like jquery.
Ps:Originally, jsdom is also an option, but unfortunately the latest version of jsdom no longer supports nodejs

The following is an example:

// This example reads the file name from filelist.txtand returns it to function getFileList () {var content = fs under the select tag with the listselect class in index.html. readFileSync ('. /index.html '); $ = cheerio. load (content); $ ('select. listselect '). empty (); // avoid duplication. Clear all sub-tags lineReader under the tag before loading. eachLine ('. /filelist.txt ', function (line, last) {// lineReader is a nodejs plug-in for reading files by line. You can install var file = line on your own. substr (line. lastIndexOf ('') + 1); $ ('select. listselect '). append ('<option class = "listoption" value =' + file + '>' + file + '</option>'); if (last) {fs. writeFile (". /index.html ", developer.html (), function (err) {if (err) throw err; console. log ("Saved") ;}); return false ;}});

Why?

$ = cheerio.load(content);

This writing method is really tangled. It must be the $ symbol or it will lead to errors. I personally understand that it seems to be consistent with the selector in jquery and it is specially written like this.

For more operations, see
Https://npmjs.org/package/cheerio
Official documents or the following translation documents
Https://cnodejs.org/topic/5203a71844e76d216a727d2e

Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.