简体   繁体   English

Internet Explorer中的jQuery隐藏/显示问题

[英]jQuery Hide/Show issue in Internet Explorer

<script type="text/javascript">
    function unhide(divID) {
        var item = document.getElementById(divID);
        if (item) {
            item.className=(item.className=='hidden')?'unhidden':'hidden';
        }
    }
</script>

I'm trying to figure out what's wrong with this code. 我试图找出这段代码出了什么问题。 It toggles hide/unhide for all browsers except IE. 它为除IE之外的所有浏览器切换隐藏/取消隐藏。 The direct site is http://lilacparadise.x10.mx/comics/ . 直接站点是http://lilacparadise.x10.mx/comics/ I'm helping a friend out but I have little experience with javascript. 我正在帮助一位朋友,但我对javascript的了解很少。 Can someone point me in the right direction to fix this code? 有人可以指出正确的方向来修复此代码吗?

i am not 100% sure about your question. 我不是100%肯定您的问题。 Instead of function i would suggent you to use toggle in jquery.remaining jquery will take care. 我建议您在jquery中使用toggle来代替功能,其余的jquery会注意。

css part. CSS部分。

.hidden
{
display:none
}

.visi
{
display:block
}

jquery part. jQuery部分。

$("#divid").click(function () {
      $(this).toggleClass("hidden");
    });
})

hope this will be helpfull 希望这会有所帮助

Sorry, I had a computer problem and had to restart. 抱歉,我遇到了计算机问题,必须重新启动。

First, if you have access to the page itself, get rid of this line : 首先,如果您有权访问页面本身,请摆脱此行

<script type="text/javascript" src="jquery-1.3.1.js"></script>

And replace it with: 并替换为:

<script type="text/javascript" src="http://code.jquery.com/jquery-1.7.2.min.js"></script>

That version of jQuery was released in January, 2009. That's ancient, and it's time to get rid of it. 那个版本的jQuery于2009年1月发布。那是古老的,是时候摆脱它了。 The $.fadeToggle() method I use below was introduced in 1.4.4. 我在下面使用的$.fadeToggle()方法是在1.4.4中引入的。

You should be able to figure out what I've done below. 您应该能够弄清楚我在下面所做的工作。 Let me know if you have any questions. 如果您有任何疑问,请告诉我。 It's not drag and drop. 不是拖放。 There's also more CSS in the fiddle demo below, but I've only included the parts relevant to the demo. 下面的小提琴演示中还有更多CSS,但我只包括了与演示相关的部分。

$(function load(){
    var $my = $('#my'),
        $comment = $my.find('.comment'),
        $siblings = $my.find('.siblings');

    $my.click(function toggle(){
        $comment.text($siblings.not(':hidden').text());

        $siblings.toggleClass('green').fadeToggle();
    });
});​

http://jsfiddle.net/userdude/ULDKg/ http://jsfiddle.net/userdude/ULDKg/

This is the CSS and markup it's working with: 这是它的CSS和标记:

.siblings.hidden {
    display: none;
}
.siblings.green {
    background: green;
}

<div id="my">
    <div class="comment">Click for the comment...</div>
    <div class="siblings green">I don't know...</div>
    <div class="siblings hidden">What is it?</div>
</div>​

I've tested in Firefox, Chrome, and IE 7-9 and it works fine. 我已经在Firefox,Chrome和IE 7-9中进行了测试,并且工作正常。 But you need v1.4.4 or higher at least. 但是您至少需要v1.4.4或更高版本。

Also note that this uses the short form of $(document).ready() . 还要注意,这使用$(document).ready()缩写形式 This means that it waits until the page has been parsed and the DOM is ready to be accessed (in it's entirety). 这意味着它要等到页面已被解析并且DOM准备好被访问(完整)后才开始。 You can use $(window).load() instead, it would work, but what you see here mimics what's already been done (which is ok). 您可以改用$(window).load() ,它可以工作,但是您在这里看到的内容模仿的是已经完成的事情(没关系)。 Just keep it in mind you need it to wait until the page has been parsed at least, or the code sans either wrapper method needs to follow the elements it's accessing in where it's included. 只要记住它,您就需要它至少等到页面被解析为止,或者代码没有使用包装方法来跟踪其所包含元素的访问方式。

className may not work properly in IE. className在IE中可能无法正常工作。 Can you try this and let me know whether it works, 您可以尝试一下,让我知道是否可行,

<script type="text/javascript">
    function unhide(divID) {
        var item = document.getElementById(divID);
        if (item) {
           if(item.getAttribute('class')=='hidden'){
               item.setAttribute('className', "unhidden") || item.setAttribute('class', "unhidden");
           }else{
               item.setAttribute('className', "hidden") || item.setAttribute('class', "hidden");
           }

        }
    }
</script>

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

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