【发布时间】:2017-05-03 04:20:22
【问题描述】:
- 我是 js 新手
- 大约 8 小时,我正在尝试调试为什么我得到以下空对象
- document.getElementsByClassName("sports-title") 在小提琴中工作正常,但是当我放入我的代码库时它不能正常工作。
-
它像这样返回,所以我无法继续。
代码库输出 运动--标题---> {} 第三个-->未定义
小提琴输出
sports-title---->{"0":{}} 第三---->{}
我使用相同的 html 结构。
- 你们能告诉我可能是什么问题,以便我继续。
findStringInsideDiv() {
var sportsTitle = document.getElementsByClassName("sports-title");
var third = sportsTitle[0];
var thirdHTML = third.innerHTML
//str = str.split(" ")[4];
console.log("sports-title---->" + JSON.stringify(sportsTitle));
console.log("third---->" + JSON.stringify(third));
console.log("thirdHTML---->" + JSON.stringify(thirdHTML));
if ( thirdHTML === " basketball football swimming " ) {
console.log("matching basketball---->");
var menu = document.querySelector('.sports');
menu.classList.add('sports-with-basketball');
// how to add this class name directly to the first div after body.
// but we are not rendering that div in accordion
//is it possible
}
else{
console.log("not matching");
}
}
【问题讨论】:
-
请注意,您的 jsfiddle 示例有一个巨大的语法错误:
React.render((<div>...</div>), ...)— 那里的 HTML 需要在字符串中,否则您将得到"SyntaxError: expected expression, got '<'"
标签: javascript html