简体   繁体   English

getElementById(“ blah”)。classname =“ whatever”不起作用

[英]getElementById(“blah”).classname = “whatever” Not working

Heya, I've got some code that I think ought to be working (though I really am fumbling around in the dark here) - I'm just trying to dynamically change a class name on a div tag. 嘿,我有一些我认为应该可以工作的代码(尽管我确实在黑暗中摸索着)-我只是想在div标签上动态更改类名。

I'm trying to change the div class on id "slider" from "visible" to "hidden". 我正在尝试将id“ slider”上的div类从“ visible”更改为“ hidden”。 Everything else about this code works just fine, but the class won't change. 关于此代码的所有其他东西都工作正常,但是类不会改变。 What am I doing wrong? 我究竟做错了什么?

I've tried checking to see if the browser was correctly evaluating for IE 6 - it is. 我尝试检查浏览器是否正确评估了IE 6-是的。 The code in the first part of the if statement that initializes slider when it's not IE6 works. if语句的第一部分中的代码在非IE6时初始化滑块。 And if I replace document.getElementById with something else, that code works too. 如果我将document.getElementById替换为其他代码,该代码也可以正常工作。 So it must be a problem with the way I'm calling getElementbyId? 所以我调用getElementbyId的方式一定有问题吗? It's just doing nothing in IE6. 在IE6中它什么也没做。

Here's what I've got: 这是我得到的:

<script type="text/javascript">


$(window).load(function() {
var isIE6 = false;
        if(/MSIE 6/i.test(navigator.userAgent)) {
          isIE6 = true;
        }

    if(!isIE6)
    {

 $('#slider').nivoSlider({
  effect:'fade', 
  slices:1,
  animSpeed:500,
  pauseTime:3000,
  startSlide:0, 
  directionNav:false,
  directionNavHide:true, 
  controlNav:true,
  controlNavThumbs:false,
      controlNavThumbsFromRel:false, 
  controlNavThumbsSearch: '.jpg', 
  controlNavThumbsReplace: '_thumb.jpg', 
  keyboardNav:true, 
  pauseOnHover:true, 
  manualAdvance:false, 
  captionOpacity:0.8
 });
 }
 else 
 {
 document.getElementById('slider').className = "hidden"; 
 }
});
</script>



<div id="slider" class="visible">
  <img src="/img/nivoslider/slide1.jpg" />
  <img src="/img/nivoslider/slide2.jpg" />
  <img src="/img/nivoslider/slide3.jpg"  />
</div><!-- end slider -->

If you're using jQuery, which it looks like you are, why not do it this way: 如果您使用的是jQuery,那么为什么不这样做呢?

$('#slider').addClass('hidden').removeClass('visible')

This also begs the question: why not use $('#slider').toggle() or $('#slider').hide() and $('#slider').show() as appropriate? 这也引出一个问题:为什么不适当地使用$('#slider').toggle()$('#slider').hide()$('#slider').show()

Just a question: How do you know the classname isn't changing? 只是一个问题:您怎么知道类名没有改变? Have you set the CSS styles to match the hidden class. 您是否设置了CSS样式以匹配隐藏的类。

On top of that, if the only purpose of the class is to change the visibility, why not just change that directly? 最重要的是,如果该类的唯一目的是更改可见性,为什么不直接更改它呢?

document.getElementById('slider').visibility = "hidden";

If all that doesn't work, and since you are already using jQuery, try using 如果所有方法都不起作用,并且由于您已经在使用jQuery,请尝试使用

$("#slider").hide();

If you want to ensure that you are chaning the class so you don't have to do an add/remove and just want to do a replace using jquery you could do: 如果您想确保自己在改变类,那么就不必进行添加/删除操作,而只想使用jquery进行替换,您可以执行以下操作:

$('#slider').attr('class', 'hidden');

or

$('#slider').attr('class', 'visible');

This way you can never get out of synch at least, it will always be just one of those values. 这样,您将永远不会至少失去同步,它将永远只是这些值之一。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM