我已经尝试了几乎所有可能的代码组合,但显然正确的组合除外。我想要做的就是在 javascript/JQuery 中通过 id 获取元素并将其显示在警报中。在阅读了一些类似的帖子后,我将脚本放在正文之后,将代码放在 document.ready、window.onload 中,但仍然没有。最终我有一个按钮和一个带有灰色背景图像的 .button css 类和带有白色背景图像的 .button:hover 。我想删除 .button 类并将其替换为具有深灰色背景的 .buttonSelected 类。时间差
<div id="PlanningNav">
<a href="~/Planning/Planning.cshtml">
<div id="AboutMe" class="Button">
<img src="~/Resources/Images/icoAbout_Me.jpg" height="20" width="@Button_Width" />
About Me
</div>
</a>
</div>
<script>
window.onload = function () {
var button = document.GetElementById('AboutMe');
alert(button);
};
$(document).ready(function () {
var button;
if (page.indexOf('About') > -1) {
button = document.GetElementById('AboutMe');
alert(button.id);
// $('#AboutMe').removeClass('.Button');
//$('#AboutMe').addClass('.ButtonSelected');
// alert(button.innerHTML);
}
});
</script>
最佳答案
JavaScript 区分大小写,所以不用
document.GetElementById('AboutMe');
你需要使用
document.getElementById('AboutMe');
关于javascript - GetElementByID 问题不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32163831/