简体   繁体   English

jQuery帮助:toggle()无法正常工作

[英]JQuery Help: toggle() is not working properly

I'm trying to use JQuery toggle functionality, but not able to use properly. 我正在尝试使用JQuery切换功能,但无法正确使用。 Instead of smooth slide up and down, it goes very fast and not in an animated manner. 它没有快速地上下滑动,而是运行得很快,并且没有动画效果。
I want to achieve sliding effect in my code, like this has (Please see Website Design, Redesign Services slider): 我想在代码中实现滑动效果,如下所示 (请参阅网站设计,重新设计服务滑块):

Here is my code: 这是我的代码:

HTML: HTML:

<div>
     <div class="jquery_inner_mid">
            <div class="main_heading">
                <a href="#">
                    <img src="features.jpg" alt="" title="" border="0" /></a>
            </div>

            <div class="plus_sign">
                <img id="imgFeaturesEx" src="images/plus.jpg" alt="" title="" border="0" />
                <img id="imgFeaturesCol" src="images/minus.jpg" alt="" title="" border="0" /></div>
            <div class="toggle_container">
                <div id="divMain" >
                </div>
            </div>
        </div>
        <div class="jquery_inner_mid">
            <div class="main_heading">
                <img src="About.jpg" alt="" title="" /></div>
            <div class="plus_sign">
                <img id="imgTechnoEx" src="images/plus.jpg" alt="" title="" border="0" />
                <img id="imgTechnoCol" src="images/minus.jpg" alt="" title="" border="0" /></div>
            <div class="toggle_container">
                <div id="divTechnossus" >
                </div>
            </div>
        </div>
    </div>

JQuery: JQuery的:

 $(function() {

            document.getElementById('imgFeaturesCol').style.display = 'none';
            document.getElementById('imgTechnoCol').style.display = 'none';


            $('#imgFeaturesEx').click(function() {


                $.getJSON("/Visitor/GetFeatureInfo", null, function(strInfo) {
                    document.getElementById('divMain').innerHTML = strInfo;
                });
                $("#divMain").toggle("slow");
                document.getElementById('imgFeaturesEx').style.display = 'none';
                document.getElementById('imgFeaturesCol').style.display = 'block';
            });
            $('#imgFeaturesCol').click(function() {
                document.getElementById('divMain').innerHTML = "";
                $("#divMain").toggle("slow");
                document.getElementById('imgFeaturesCol').style.display = 'none';
                document.getElementById('imgFeaturesEx').style.display = 'block';
            });

            $('#imgTechnoEx').click(function() {
                $.getJSON("/Visitor/GetTechnossusInfo", null, function(strInfo) {
                    document.getElementById('divTechnossus').innerHTML = strInfo;
                });
                $("#divTechnossus").slideToggle("slow");
                document.getElementById('imgTechnoEx').style.display = 'none';
                document.getElementById('imgTechnoCol').style.display = 'block';
            });
            $('#imgTechnoCol').click(function() {
                document.getElementById('divTechnossus').innerHTML = "";
                $("#divTechnossus").slideToggle("slow");
                document.getElementById('imgTechnoCol').style.display = 'none';
                document.getElementById('imgTechnoEx').style.display = 'block';
            });
  });

Edit: I also want to optimize this code (coz code is not very clean + number of line may also be reduce). 编辑:我也想优化此代码(因为COZ代码不是很干净+行数也可能减少)。 I don't know about the correct coding standards for JQuery. 我不知道有关JQuery的正确编码标准。 I'm very new in JQuery guys, so please show me the right path so that I can optimize this stupid code. 我是JQuery的新手,所以请向我展示正确的路径,以便我可以优化此愚蠢的代码。

I could not resist optimizing your code for use with jQuery . 我无法抗拒优化与jQuery一起使用的代码。

It puzzled me as to why you had all those getElementById calls in there when you were already including jQuery 这让我感到困惑,为什么当您已经包含jQuery时,为什么在其中拥有所有这些getElementById调用

Try this: 尝试这个:

( function() {
        $( [ '#imgFeaturesCol', '#imgTechnoCol' ] ).hide();

        $('#imgFeaturesEx').click(function() {
            $.getJSON("/Visitor/GetFeatureInfo", null, function(strInfo) {
                $( '#divMain' ).html( strInfo )
                               .slideToggle( "slow" );
            });
            $( '#imgFeaturesEx' ).hide();
            $( '#imgFeaturesCol' ).show();
        });
        $('#imgFeaturesCol').click(function() {
            $( '#divMain' ).html( "" )
                           .slideToggle( "slow" );
            $( '#imgFeaturesCol' ).hide();
            $( '#imgFeaturesEx' ).show();
        });

        $('#imgTechnoEx').click(function() {
            $.getJSON("/Visitor/GetTechnossusInfo", null, function(strInfo) {
               $( '#divTechnossus').html( strInfo )
                                   .slideToggle( "slow" );
            });
            $( '#imgTechnoEx' ).hide();
            $( '#imgTechnoCol' ).show();
        });
        $('#imgTechnoCol').click(function() {
            $( '#divTechnossus').html( "" )
                                .slideToggle( "slow" );
            $( '#imgTechnoCol' ).hide();
            $( '#imgTechnoEx' ).show();            
        });
})();

Use slideToggle('slow'); 使用slideToggle('slow'); intead of toggle(); toggle();

Jacob Relkin has given you a good alternative for your code, I take the opportunity to give to the viewers of this thread some rules to the passage from javascript coding to jquery coding: Jacob Relkin为您的代码提供了一个很好的选择,我借此机会为该线程的查看者提供了一些规则,以指导从javascript编码到jquery编码的转换:

1-replace all document.getElementById('element') by $('element') 将所有document.getElementById('element')替换为$('element')

2-for all css styles, you use the function $('element').css("name","value"); 2-对于所有css样式,都使用函数$('element').css("name","value");

3-for the hide and show on a same element, you can use the toggle or slideToggle function and theirs effects (slow, fast..) 3-对于在同一元素上的隐藏和显示,可以使用toggleslideToggle函数及其效果(慢,快..)

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

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