document.getElementById 没有返回任何值

用户8055794

我正在玩 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”

LF00

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] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

document.getElementById()没有获得值

document.getElementById返回空

document.getElementById()返回null

document.getElementById返回null

document.getElementById 返回 HTMLDivElement {}

所有 document.getElementById('') 返回 null

Javascript Document.getElementById返回null

离子2 document.getElementById返回null

document.getElementById 始终返回 null

document.getelementbyid在循环中返回null

具有ClientID的UserControl的document.getElementById

我没有从 document.getElementById(id).value 获得任何价值

document.getElementById 没有写我告诉它做的

PHP带有document.getElementById的选项标签中的值

如何在没有 document.getElementById('..') 的情况下获取输入(文本)字段的值

为什么document.getElementById返回一个具有名为“值”的属性的对象?

document.getElementById与jQuery $()

Javascript document.getElementById

document.getElementById('').value返回未定义

为什么document.getElementById在这里返回null?

无法使用document.getElementById获取元素,返回null

使用 document.getElementById 時返回 [object HTMLDivElement]

如何从document.getElementByID('something')。value获取空值

JavaScript登录表单document.getElementById未检测到值

使用 JavaScript document.getElementById 方法得到“缺失值”结果

我无法使用 document.getElementById() 获取 <texterea /> 的值

无法从 document.getElementById 读取 null 的属性(读取“值”)

Typescript与document.getElementbyID有什么关系?

在具有 document.getElementById 的测试中渲染组件