Jquery 当元素没有附加类时的事件处理程序

Jquery 当元素没有附加类时的事件处理程序,jquery,wordpress,event-handling,Jquery,Wordpress,Event Handling,我需要一个附加到元素的事件处理程序,默认情况下该元素是不可见的 <div id="help" class="hidden"> <div id="my-help-tab" class="hidden"> <div class="targeted"> <a href="#" class="show-hide">Show</a> <pre>test&l

我需要一个附加到元素的事件处理程序,默认情况下该元素是不可见的

<div id="help" class="hidden">
     <div id="my-help-tab" class="hidden">
         <div class="targeted">
             <a href="#" class="show-hide">Show</a>
             <pre>test</pre>
         </div>
     </div>
</div>
jQuery( document ).ready( function($)
{
    $( '.targeted' ).on( 'click', function( e )
    {
        event.preventDefault();
        $( this ).children( '.show-hide' ).on( 'click', function( e )
        {
            $( this ).toggle(
                function( e ) 
                {
                    $( this ).html( 'Hide' );
                    $( this ).next( 'pre' ).toggle();
                },
                function()
                {
                    $( this ).html( 'Show' );
                    $( this ).next( 'pre' ).toggle();
                }
            );
        } );
    } );
} );
在那里,您可以找到几个jQuery选项卡,我的目标选项卡在默认情况下也是不可见的

<div id="help" class="hidden">
     <div id="my-help-tab" class="hidden">
         <div class="targeted">
             <a href="#" class="show-hide">Show</a>
             <pre>test</pre>
         </div>
     </div>
</div>
jQuery( document ).ready( function($)
{
    $( '.targeted' ).on( 'click', function( e )
    {
        event.preventDefault();
        $( this ).children( '.show-hide' ).on( 'click', function( e )
        {
            $( this ).toggle(
                function( e ) 
                {
                    $( this ).html( 'Hide' );
                    $( this ).next( 'pre' ).toggle();
                },
                function()
                {
                    $( this ).html( 'Show' );
                    $( this ).next( 'pre' ).toggle();
                }
            );
        } );
    } );
} );
你可以:

jQuery( document ).ready( function($)
{
    $( '.targeted' ).on( 'click', function( e )
    {
        event.preventDefault();
        $( this ).children( '.show-hide' ).on( 'click', function( e )
        {
            $( this ).toggle(
                function( e ) 
                {
                    $( this ).html( 'Hide' );
                    $( this ).next( 'pre' ).toggle();
                },
                function()
                {
                    $( this ).html( 'Show' );
                    $( this ).next( 'pre' ).toggle();
                }
            );
        } );
    } );
} );
$('#help:not(.hidden) .targeted > a')
或:

jQuery( document ).ready( function($)
{
    $( '.targeted' ).on( 'click', function( e )
    {
        event.preventDefault();
        $( this ).children( '.show-hide' ).on( 'click', function( e )
        {
            $( this ).toggle(
                function( e ) 
                {
                    $( this ).html( 'Hide' );
                    $( this ).next( 'pre' ).toggle();
                },
                function()
                {
                    $( this ).html( 'Show' );
                    $( this ).next( 'pre' ).toggle();
                }
            );
        } );
    } );
} );
或者(但可能不必要):

jQuery( document ).ready( function($)
{
    $( '.targeted' ).on( 'click', function( e )
    {
        event.preventDefault();
        $( this ).children( '.show-hide' ).on( 'click', function( e )
        {
            $( this ).toggle(
                function( e ) 
                {
                    $( this ).html( 'Hide' );
                    $( this ).next( 'pre' ).toggle();
                },
                function()
                {
                    $( this ).html( 'Show' );
                    $( this ).next( 'pre' ).toggle();
                }
            );
        } );
    } );
} );
另一种没有等级的方法:

jQuery( document ).ready( function($)
{
    $( '.targeted' ).on( 'click', function( e )
    {
        event.preventDefault();
        $( this ).children( '.show-hide' ).on( 'click', function( e )
        {
            $( this ).toggle(
                function( e ) 
                {
                    $( this ).html( 'Hide' );
                    $( this ).next( 'pre' ).toggle();
                },
                function()
                {
                    $( this ).html( 'Show' );
                    $( this ).next( 'pre' ).toggle();
                }
            );
        } );
    } );
} );
您应该阅读以下文档:

jQuery( document ).ready( function($)
{
    $( '.targeted' ).on( 'click', function( e )
    {
        event.preventDefault();
        $( this ).children( '.show-hide' ).on( 'click', function( e )
        {
            $( this ).toggle(
                function( e ) 
                {
                    $( this ).html( 'Hide' );
                    $( this ).next( 'pre' ).toggle();
                },
                function()
                {
                    $( this ).html( 'Show' );
                    $( this ).next( 'pre' ).toggle();
                }
            );
        } );
    } );
} );
    jQuery( document ).ready( function($)
    {
        $( '.targeted' ).on( 'click', function( e )
        {
            event.preventDefault();
            $( this ).children( '.show-hide' ).on( 'click', function( e )
            {
                $( this ).toggle(
                    function( e ) 
                    {
                        $( this ).html( 'Hide' );
                        $( this ).next( 'pre' ).toggle();
                    },
                    function()
                    {
                        $( this ).html( 'Show' );
                        $( this ).next( 'pre' ).toggle();
                    }
                );
            } );
        } );
    } );
    
  • jQuery( document ).ready( function($)
    {
        $( '.targeted' ).on( 'click', function( e )
        {
            event.preventDefault();
            $( this ).children( '.show-hide' ).on( 'click', function( e )
            {
                $( this ).toggle(
                    function( e ) 
                    {
                        $( this ).html( 'Hide' );
                        $( this ).next( 'pre' ).toggle();
                    },
                    function()
                    {
                        $( this ).html( 'Show' );
                        $( this ).next( 'pre' ).toggle();
                    }
                );
            } );
        } );
    } );
    
  • jQuery( document ).ready( function($)
    {
        $( '.targeted' ).on( 'click', function( e )
        {
            event.preventDefault();
            $( this ).children( '.show-hide' ).on( 'click', function( e )
            {
                $( this ).toggle(
                    function( e ) 
                    {
                        $( this ).html( 'Hide' );
                        $( this ).next( 'pre' ).toggle();
                    },
                    function()
                    {
                        $( this ).html( 'Show' );
                        $( this ).next( 'pre' ).toggle();
                    }
                );
            } );
        } );
    } );
    
  • jQuery( document ).ready( function($)
    {
        $( '.targeted' ).on( 'click', function( e )
        {
            event.preventDefault();
            $( this ).children( '.show-hide' ).on( 'click', function( e )
            {
                $( this ).toggle(
                    function( e ) 
                    {
                        $( this ).html( 'Hide' );
                        $( this ).next( 'pre' ).toggle();
                    },
                    function()
                    {
                        $( this ).html( 'Show' );
                        $( this ).next( 'pre' ).toggle();
                    }
                );
            } );
        } );
    } );
    
  • /
  • jQuery( document ).ready( function($)
    {
        $( '.targeted' ).on( 'click', function( e )
        {
            event.preventDefault();
            $( this ).children( '.show-hide' ).on( 'click', function( e )
            {
                $( this ).toggle(
                    function( e ) 
                    {
                        $( this ).html( 'Hide' );
                        $( this ).next( 'pre' ).toggle();
                    },
                    function()
                    {
                        $( this ).html( 'Show' );
                        $( this ).next( 'pre' ).toggle();
                    }
                );
            } );
        } );
    } );
    
jQuery( document ).ready( function($)
{
    $( '.targeted' ).on( 'click', function( e )
    {
        event.preventDefault();
        $( this ).children( '.show-hide' ).on( 'click', function( e )
        {
            $( this ).toggle(
                function( e ) 
                {
                    $( this ).html( 'Hide' );
                    $( this ).next( 'pre' ).toggle();
                },
                function()
                {
                    $( this ).html( 'Show' );
                    $( this ).next( 'pre' ).toggle();
                }
            );
        } );
    } );
} );
有关子代选择器与子代选择器的更多说明,请阅读

jQuery( document ).ready( function($)
{
    $( '.targeted' ).on( 'click', function( e )
    {
        event.preventDefault();
        $( this ).children( '.show-hide' ).on( 'click', function( e )
        {
            $( this ).toggle(
                function( e ) 
                {
                    $( this ).html( 'Hide' );
                    $( this ).next( 'pre' ).toggle();
                },
                function()
                {
                    $( this ).html( 'Show' );
                    $( this ).next( 'pre' ).toggle();
                }
            );
        } );
    } );
} );