MJo*_*Job -4 html javascript alert getelementbyid
开发者,我试图让我的javascript文件捕获我的HTML文件中的id的innerHTML并提醒它.我的html中的src代码到js很好,因为它可以提醒任何我引用它的警报.它不会提醒我想要的内容.我做得不对劲?
这是我的html代码(我已缩短为仅显示相关代码)
<head>
<script type="text/javascript" src="indexjs.js"></script>
</head>
<body>
<div class="topbar">
<ul>
<li id="barmuhia">Job Muhia</li>
</ul>
</div>
</body>
Run Code Online (Sandbox Code Playgroud)
实际在我的网页上工作的js代码是
alert(GetElementById("barmuhia").innerHTML)
Run Code Online (Sandbox Code Playgroud)
一个不能工作的是这个(也不适用于任何其他id)
<head>
<script type="text/javascript" src="indexjs.js"></script>
</head>
<body>
<div class="topbar">
<ul>
<li id="barmuhia">Job Muhia</li>
</ul>
</div>
</body>
Run Code Online (Sandbox Code Playgroud)
@brandonoluoch
实际的方法名称是getElementById,它位于文档对象中.见文档.
将GetElementById第一个字母改为小写字母并将前缀改为文档 - document.getElementById.
例
alert("js file connected");
alert(document.getElementById("barmuhia").innerHTML);Run Code Online (Sandbox Code Playgroud)
<body>
<div class="topbar">
<ul>
<li id="barmuhia">Job Muhia</li>
</ul>
</div>
</body>Run Code Online (Sandbox Code Playgroud)
更新
index.js
window.onload = function () {
// your code goes here
}
Run Code Online (Sandbox Code Playgroud)
要么
<head>
</head>
<body>
<div class="topbar">
<ul>
<li id="barmuhia">Job Muhia</li>
</ul>
</div>
<script type="text/javascript" src="indexjs.js"></script>
</body>
Run Code Online (Sandbox Code Playgroud)