在控制台中记录的对象长度为0但不为空

时间:2018-01-25 19:56:09

标签: javascript arrays google-chrome-extension

我正在制作Chrome扩展程序来修改页面上的HTML。这就是我所拥有的:

的manifest.json

{
  "manifest_version": 2,

  "name": "Trello YouTube video embed",
  "version": "1.0",
  "description": "Embeds YouTube video players in Trello card descriptions based on links in the card description",
  "content_scripts": [{
    "js": ["embed.js"],
    "matches": ["https://trello.com/*"],
    "run_at": "document_end"
  }]
}

embed.js

var cardDesc = document.getElementsByClassName('current markeddown hide-on-edit js-card-desc js-show-with-desc');
console.log(cardDesc);
console.log('cardDesc === undefined : ' + (cardDesc === undefined).toString());
console.log('cardDesc[0] === undefined : ' + (cardDesc[0] === undefined).toString());
console.log('cardDesc.length : ' + cardDesc.length);

这是控制台的屏幕截图:https://i.imgur.com/w8UbmBg.png

问题是cardDesc返回0的长度,但我可以在HTMLCollection中清楚地看到一个项目。有什么建议我可以采用不同的方式来操纵cardDesc中的项目吗?

1 个答案:

答案 0 :(得分:0)

尝试使用 document.querySelectorAll('.current.markeddown.hide-on-edit.js-card-desc.js-show-with-desc')

而不是 document.getElementsByClassName

相关问题