IE8 下无法获取标签的innerHTML
我正在尝试获取类名 current_page_item[0] 的innerhtml...这在 FF 甚至 IE9 中都工作得很好。但在 IE 8 中,“var classelem=document.getElementsByClassName('current_page_item')[0].innerHTML;
”行中似乎存在一些 javascript 错误。
我尝试在上述行之后添加警报。但是它没有显示消息“Hello Again”。
您知道如何解决此问题吗? document.getElementsByClassName
在 IE8 中不起作用吗?
<html>
<head>
<script type="text/javascript">
function updatesidebar()
{
alert("Hello");
var classelem=document.getElementsByClassName('current_page_item')[0].innerHTML;
alert("Hello again");
}
</script>
</head>
<body>
<div id="main">
<div class="menu_main">
<ul class='mainmenu' id='root'>
<li><a href="/home" class="">Home</a></li>
<li><a href="/solutions" class="">Solutions</a>
</li><li><a href="/services" class="">Services</a></li>
<li><a href="/about-us" class="current_page_item">About Us</a></li>
<li><a href="/news" class="">News and Events</a></li>
<li><a href="/careers" class="">Careers</a></li>
<li><a href="/contact-us" class="">Contact Us</a></li>
</ul>
</div>
</div>
<script type="text/javascript">
window.onload=updatesidebar();
</script>
</body>
</html>
I am trying to get the innerhtml of class name current_page_item[0]... And this is working fine in FF and even in IE9 also. But in IE 8 it seems to be some javascript error in line" var classelem=document.getElementsByClassName('current_page_item')[0].innerHTML;
.
I tried to put alert after the above line. But it is not displaying the message "Hello again".
Any idea how to solve the browser issue for this? Is it something that document.getElementsByClassName
wont work in IE8?
<html>
<head>
<script type="text/javascript">
function updatesidebar()
{
alert("Hello");
var classelem=document.getElementsByClassName('current_page_item')[0].innerHTML;
alert("Hello again");
}
</script>
</head>
<body>
<div id="main">
<div class="menu_main">
<ul class='mainmenu' id='root'>
<li><a href="/home" class="">Home</a></li>
<li><a href="/solutions" class="">Solutions</a>
</li><li><a href="/services" class="">Services</a></li>
<li><a href="/about-us" class="current_page_item">About Us</a></li>
<li><a href="/news" class="">News and Events</a></li>
<li><a href="/careers" class="">Careers</a></li>
<li><a href="/contact-us" class="">Contact Us</a></li>
</ul>
</div>
</div>
<script type="text/javascript">
window.onload=updatesidebar();
</script>
</body>
</html>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
尽管情况正在改善,但并非所有浏览器都原生支持
getElementsByClassName
。您可以使用一个函数来检查本机实现并在找到时使用它,否则获取所有元素并检查每个元素的类名,返回一组匹配的元素。Not all browsers natively support
getElementsByClassName
although the situation is improving. You could use a function which checks for the native implementation and uses it if found or else grabs all the elements and checks each one for the classname, returning an array of those that match.getElementsByClassName
与 IE8 不兼容。它是 HTML5 的一部分getElementsByClassName
is not compatible in IE8. it's part of HTML5将:更改
为:
您刚才的方式将立即调用该函数,而不是在加载页面时调用该函数。
Change:
to:
The way you have it just now will call the function immediately rather than when the page is loaded.
getElementsByClassName 不是 JS 本机函数,您必须引用包含它的任何库
getElementsByClassName is not a JS native function you must refer to any library including it
您不必更改代码,但如果不存在,您可以添加该函数...;)
you don't have to change the code, but you may add that function if not exists... ;)