Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/400.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript jQuery集合类在其已设置后不工作_Javascript_Jquery_Html_Css_Class - Fatal编程技术网

Javascript jQuery集合类在其已设置后不工作

Javascript jQuery集合类在其已设置后不工作,javascript,jquery,html,css,class,Javascript,Jquery,Html,Css,Class,尝试制作一个简单的扩展标题脚本 我不想用手风琴,只是想找一个重量轻的自制解决方案。因为我喜欢自己写作和学习 在我看来,我所拥有的应该有用。但事实并非如此 目的是: 单击标题时,所有内容都将隐藏,然后显示标题后的下一个内容元素。这可以防止在任何时候显示多个内容 在此之后,div类被更改为“selected”状态 这很好用 但是,如果标题类是选定状态,则下一个零件将运行,如果是,则应将其类更改回正常状态,并隐藏下一个元素内容 其目的是允许隐藏/显示选项 但是,将类更改回原来的后一部分不起作用。我也知

尝试制作一个简单的扩展标题脚本

我不想用手风琴,只是想找一个重量轻的自制解决方案。因为我喜欢自己写作和学习

在我看来,我所拥有的应该有用。但事实并非如此

目的是:

单击标题时,所有内容都将隐藏,然后显示标题后的下一个内容元素。这可以防止在任何时候显示多个内容

在此之后,div类被更改为“selected”状态

这很好用

但是,如果标题类是选定状态,则下一个零件将运行,如果是,则应将其类更改回正常状态,并隐藏下一个元素内容

其目的是允许隐藏/显示选项

但是,将类更改回原来的后一部分不起作用。我也知道写这篇文章有一个非常有效的方法,但不知道如何写

JS:

HTML示例:

<p class="headingHelp">Content Heading</p>
<div class="infoHelp">
Content 
</div>

<p class="headingHelp">Content Heading 2</p>
<div class="infoHelp">
Content 2
</div>
内容标题

内容

内容标题2

内容2 JSFiddle:

提前谢谢

由于您的“selected”类是在加载DOM之后添加的,jQuery并不知道它

我建议使用jQuery的
on()
for。这将允许您选择动态生成的类:

$(document).on('click','.headingHelp',function(){
    $('.infoHelp').fadeOut();
    $(this).next('.infoHelp').fadeIn();
    $(this).attr('class', 'headingHelp_sel');
});

$(document).on('click','.headingHelp_sel',function(){
    $(this).next('.infoHelp').fadeOut();
    $(this).attr('class', 'headingHelp');
});

编辑: 这里有另一个不使用委托的方法。它只是添加/删除一个“sel”类,而不是完全更改该类

$('.headingHelp').click(function(){

    // save clicked element in a variable for use below
    $this=$(this);  

    // remove / add "selected" class
    $('.headingHelp').removeClass('sel');
    $this.addClass('sel');

    // fade in / out content
    $('.infoHelp').fadeOut();
    $this.next('.infoHelp').stop().fadeIn();

});

由于您的“selected”类是在加载DOM之后添加的,因此jQuery不知道它

我建议使用jQuery的
on()
for。这将允许您选择动态生成的类:

$(document).on('click','.headingHelp',function(){
    $('.infoHelp').fadeOut();
    $(this).next('.infoHelp').fadeIn();
    $(this).attr('class', 'headingHelp_sel');
});

$(document).on('click','.headingHelp_sel',function(){
    $(this).next('.infoHelp').fadeOut();
    $(this).attr('class', 'headingHelp');
});

编辑: 这里有另一个不使用委托的方法。它只是添加/删除一个“sel”类,而不是完全更改该类

$('.headingHelp').click(function(){

    // save clicked element in a variable for use below
    $this=$(this);  

    // remove / add "selected" class
    $('.headingHelp').removeClass('sel');
    $this.addClass('sel');

    // fade in / out content
    $('.infoHelp').fadeOut();
    $this.next('.infoHelp').stop().fadeIn();

});

由于您的“selected”类是在加载DOM之后添加的,因此jQuery不知道它

我建议使用jQuery的
on()
for。这将允许您选择动态生成的类:

$(document).on('click','.headingHelp',function(){
    $('.infoHelp').fadeOut();
    $(this).next('.infoHelp').fadeIn();
    $(this).attr('class', 'headingHelp_sel');
});

$(document).on('click','.headingHelp_sel',function(){
    $(this).next('.infoHelp').fadeOut();
    $(this).attr('class', 'headingHelp');
});

编辑: 这里有另一个不使用委托的方法。它只是添加/删除一个“sel”类,而不是完全更改该类

$('.headingHelp').click(function(){

    // save clicked element in a variable for use below
    $this=$(this);  

    // remove / add "selected" class
    $('.headingHelp').removeClass('sel');
    $this.addClass('sel');

    // fade in / out content
    $('.infoHelp').fadeOut();
    $this.next('.infoHelp').stop().fadeIn();

});

由于您的“selected”类是在加载DOM之后添加的,因此jQuery不知道它

我建议使用jQuery的
on()
for。这将允许您选择动态生成的类:

$(document).on('click','.headingHelp',function(){
    $('.infoHelp').fadeOut();
    $(this).next('.infoHelp').fadeIn();
    $(this).attr('class', 'headingHelp_sel');
});

$(document).on('click','.headingHelp_sel',function(){
    $(this).next('.infoHelp').fadeOut();
    $(this).attr('class', 'headingHelp');
});

编辑: 这里有另一个不使用委托的方法。它只是添加/删除一个“sel”类,而不是完全更改该类

$('.headingHelp').click(function(){

    // save clicked element in a variable for use below
    $this=$(this);  

    // remove / add "selected" class
    $('.headingHelp').removeClass('sel');
    $this.addClass('sel');

    // fade in / out content
    $('.infoHelp').fadeOut();
    $this.next('.infoHelp').stop().fadeIn();

});

jQuery选择器“在当前时刻”工作。运行此代码时,选择器$('.headingHelp_sel')为空

最佳代码:

$(function() {
    $('.headingHelp').click(function(){
        var open = $(this).next('.infoHelp').is(':visible');
        $('.infoHelp').fadeOut();

        if(!open)
        {
            $(this).next('.infoHelp').fadeIn();
        }
    }); 
});
jQuery选择器“在当前时刻”工作。运行此代码时,选择器$('.headingHelp_sel')为空

最佳代码:

$(function() {
    $('.headingHelp').click(function(){
        var open = $(this).next('.infoHelp').is(':visible');
        $('.infoHelp').fadeOut();

        if(!open)
        {
            $(this).next('.infoHelp').fadeIn();
        }
    }); 
});
jQuery选择器“在当前时刻”工作。运行此代码时,选择器$('.headingHelp_sel')为空

最佳代码:

$(function() {
    $('.headingHelp').click(function(){
        var open = $(this).next('.infoHelp').is(':visible');
        $('.infoHelp').fadeOut();

        if(!open)
        {
            $(this).next('.infoHelp').fadeIn();
        }
    }); 
});
jQuery选择器“在当前时刻”工作。运行此代码时,选择器$('.headingHelp_sel')为空

最佳代码:

$(function() {
    $('.headingHelp').click(function(){
        var open = $(this).next('.infoHelp').is(':visible');
        $('.infoHelp').fadeOut();

        if(!open)
        {
            $(this).next('.infoHelp').fadeIn();
        }
    }); 
});
没想过要用。我真傻!当我可以的时候,谢谢你的想法会被标记为正确的:)我没想过要用!当我可以的时候,谢谢你的想法会被标记为正确的:)我没想过要用!当我可以的时候,谢谢你的想法会被标记为正确的:)我没想过要用!谢谢你,当我可以的时候,你会认为我是正确的:)