小编Gia*_*oco的帖子

JavaScript ChildNodes 未定义类型错误?

你好,我是编码新手,有一个通用问题,我到处找,找不到解决方案。我正在关注一个 javascript 教程并遇到了这行特定的代码。childnode 声明属性 'backgroundColor' 未定义,我不知道为什么。

错误:“未捕获的类型错误:无法设置未定义的属性‘backgroundColor’”

<!doctype html>
<html>
 <head>
 </head>
 <body>


 <div id = "sampDiv">

  <p> This is a txt field </p>

  <p> This is another txt field </p>

  </div>



  <script>

  var sampDiv = document.getElementById("sampDiv");

  sampDiv.childNodes[0].style.backgroundColor = "red";
</script> 


</body>
</html>
Run Code Online (Sandbox Code Playgroud)

javascript types undefined child-nodes

6
推荐指数
1
解决办法
2126
查看次数

为什么 Rust 中不能推断生命周期?

考虑下面的例子

struct Foo {
    val: &str
}

fn main() {
    let hello = String::from("hello");
    let foo = Foo{ val: &hello[..]};

}
Run Code Online (Sandbox Code Playgroud)

这不会编译,因为需要生命周期。一个非常简单的修复如下:

struct Foo<'a> {
    val: &'a str
}
Run Code Online (Sandbox Code Playgroud)

为什么编译器不能假设(作为合理的默认值)引用的寿命与 struct 一样长Foo?是否有任何用例不会出现这种情况?

lifetime rust

2
推荐指数
1
解决办法
93
查看次数

标签 统计

child-nodes ×1

javascript ×1

lifetime ×1

rust ×1

types ×1

undefined ×1