ReferenceError:未定义“ getElementsByClassName”

时间:2019-12-16 15:44:07

标签: javascript google-apps-script getelementsbyclassname urlfetchapp

嗨,当我尝试运行此代码时,我一直收到错误消息,说:

  

“ getElementsByClassName”未定义。

function doGet() {
  var html = UrlFetchApp.fetch('http://en.wikipedia.org/wiki/Document_Object_Model').getContentText();
  var doc = XmlService.parse(html);
  var html = doc.getRootElement();
  var menu = getElementsByClassName(html, 'vertical-navbox nowraplinks')[0];
  var output = XmlService.getRawFormat().format(menu);
  return HtmlService.createHtmlOutput(output);
}  

知道为什么吗?

2 个答案:

答案 0 :(得分:2)

替换

var menu = getElementsByClassName(html, 'vertical-navbox nowraplinks')[0];

使用

var menu = doc.getElementsByClassName('vertical-navbox nowraplinks')[0];

答案 1 :(得分:0)

https://sites.google.com/site/scriptsexamples/learn-by-example/parsing-html中的getElementByIdgetElementsByClassNamegetElementsByTagName函数定义添加到脚本中