你好,我是编码新手,有一个通用问题,我到处找,找不到解决方案。我正在关注一个 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) 考虑下面的例子
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?是否有任何用例不会出现这种情况?