我正在玩 TypeScript 并尝试创建一个脚本,该脚本将获取具有 class 和 id 的 div 的 HTML 值。
但是,我能够获得课程的结果,但无法使用getElementById
.
html 如下所示:
<button (click)="selectDiv()">select div</button>
<div class="container" id="main-wrapper">
<p>
Fetch me
</p>
</div>
和explore.ts
文件
selectDiv() {
//create a new HTMLElement from nativeElement
var hElement: HTMLElement = this.elRef.nativeElement;
//now you can simply get your elements with their class name
var allDivs = hElement.getElementsByClassName('container')[0];
var getSection = <HTMLElement>document.getElementById["main-wrapper"].innerHTML;
console.log(allDivs);
console.log(getSection); // returning nothing
}
getsection
给我一个错误如下:
错误类型错误:无法读取未定义的属性“innerHTML”
function selectDiv() {
//create a new HTMLElement from nativeElement
// var hElement: HTMLElement = this.elRef.nativeElement;
//now you can simply get your elements with their class name
var allDivs = document.getElementsByClassName('container')[0];
var getSection = document.getElementById("main-wrapper").innerHTML;
console.log(allDivs);
console.log(getSection); // returning nothing
}
<button onclick="selectDiv()">select div</button>
<div class="container" id="main-wrapper">
<p>
Fetch me
</p>
</div>
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句