且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何使用node.js在cheerio中获得元素名称

更新时间:2023-08-19 08:49:58

只有我想一种情况是,当 $ someElement.attr('name')返回 undefined 时-如果没有 attribute name 。例如...

There's only one case, I suppose, when $someElement.attr('name') returns undefined - if there's NO attribute name on that element. For example...

var cheerio = require('cheerio'),
    $ = cheerio.load(
      '<input id="one" type="input" /><input id="two" name="some_name" />');

console.log( $('#one').attr('name') ); // undefined
console.log( $('#two').attr('name') ); // some_name

请注意,< name> 属性仅适用于以下元素集(MDN):

Note that <name> attribute is only applicable to the following set of elements (MDN):

<a>, <applet>, <button>, <form>, <frame>, <iframe>, <img>, 
<input>, <map>, <meta>, <object>, <param>, <select>, <textarea>






获取元素本身的名称(实际上 tagName ,但是Cheerio对其进行抽象),使用包装在Cheerio容器中的基础元素的 name 属性,例如:

console.log( $('#one')[0].name ); // input 
console.log( $('#two')[0].name ); // input