简体   繁体   English

您将如何使这些按钮起作用?

[英]How would you go about making these buttons work?

My problem: http://www.danieldoktor.dk/test2/test2.html 我的问题: http : //www.danieldoktor.dk/test2/test2.html

when you push either of the settings buttons it triggers both of the boxes. 当您按下任一设​​置按钮时,将同时触发两个框。

I would like an overall code to control the button and the slide-down-and-up-box with a code similar to this, which controls each of the boxes individually instead of controlling them both: 我想要一个总体代码来控制按钮和上下滑动框,其代码与此类似,它分别控制每个框而不是同时控制它们:

$(".someBtnClass").click(function () { 
        if(!$(".someBoxClass").hasClass('header-down')){
            $(".someBoxClass").stop().animate({height:'100px'},{queue:false, duration:600, easing: 'linear'}).addClass('header-down'); 
        }
        else{
            $(".someBoxClass").stop().animate({height:'30px'},{queue:false, duration:600, easing: 'linear'}).removeClass('header-down');
        }

    }); 


    $(document).click(function() {
        if($(".someBoxClass").hasClass('header-down')){
            $(".someBoxClass").stop().animate({height:'30px'},{queue:false, duration:600, easing: 'linear'}).removeClass('header-down');
    }
    });

    $(".someBoxClass").click(function(e) {
        e.stopPropagation(); // This is the preferred method.
               // This should not be used unless you do not want
                             // any click events registering inside the div
    });

Can this be done with a class? 可以上课吗?

If not, how can it be made, so the boxes, later can be loaded with php? 如果没有,怎么做,所以盒子,以后可以用php加载?

EDIT 编辑

My markup: HTML 我的标记:HTML

<div class="lists">
        <header class="box_header" id="box1">
            <h1>HEADER 1</h1>
            <div class="setting" id="btn1"></div>
            </header>
 </div>

 <div class="lists">
        <header class="box_header" id="box2">
            <h1>HEADER 2</h1>
            <div class="setting" id="btn2"></div>
            </header>
 </div>

jQuery jQuery的

$(".setting").each(function(){
    $(this).on("click", function(){if(!$(".box_header").hasClass('header-down')){
        $(".box_header").stop().animate({height:'100px'},{queue:false, duration:600, easing: 'linear'}).addClass('header-down'); 
    }
    else{
        $(".box_header").stop().animate({height:'30px'},{queue:false, duration:600, easing: 'linear'}).removeClass('header-down');
    }});
});


$(document).click(function() {
    if($(".box_header").hasClass('header-down')){
        $(".box_header").stop().animate({height:'30px'},{queue:false, duration:600, easing: 'linear'}).removeClass('header-down');
}
});

$(".box_header").click(function(e) {
    e.stopPropagation(); // This is the preferred method.
           // This should not be used unless you do not want
                         // any click events registering inside the div
});

You're selector is grabbing both. 您正在选择两者。 You need to tell it to only grab the parent and animate it, instead of grabbing the "box-header" class and animating it 您需要告诉它仅获取父对象并为其设置动画,而不是获取“ box-header”类并对其进行动画处理

Javascript: Javascript:

$('.setting').on('click', function() {
    var parent = $(this).parent();
    if (!parent.hasClass('header-down')) {
        parent.stop().animate({height:'100px'},{queue:false, duration:600, easing: 'linear'}).addClass('header-down'); 
    } else {
        parent.stop().animate({height:'30px'},{queue:false, duration:600, easing: 'linear'}).removeClass('header-down');
    }
});

JSFiddle: http://jsfiddle.net/jeffshaver/kHV8V/1/ JSFiddle: http : //jsfiddle.net/jeffshaver/kHV8V/1/

whithout knowing your markup you can try something like this: 在不知道标记的情况下,您可以尝试执行以下操作:

var box = $(this).parents("div.parent_class").find('.someBoxClass');

then replace $('.someBoxClass') with box 然后将$('。someBoxClass')替换为box

Your markup is like this (you should have added this to your question) 您的标记是这样的(您应该将此添加到问题中)

<header id="box1" class="box_header">
   <h1>HEADER 1</h1>
   <div id="btn1" class="setting"></div>
</header>

for each box. 每个盒子。 So if you click on .setting you want to animate only the .box_header (parent) of the correct settings button, I presume? 因此,如果您单击.setting您只想为正确设置按钮的.box_header (父级)设置动画,我猜想吗?

$(".setting").on("click", function(){
   if(!$(this).parent().hasClass('header-down')){
      $(this).parent().stop().animate({height:'100px'},{queue:false, duration:600, easing: 'linear'}).addClass('header-down');
   } else{
      $(this).parent().stop().animate({height:'30px'},{queue:false, duration:600, easing: 'linear'}).removeClass('header-down');
   }
});

}); });

Modified version of your jQuery Code jQuery代码的修改版本

// widget 1

 $("#btn1").each(function(){
    $(this).on("click", function(){if(!$(".box_header").hasClass('header-down')){
        $("#box1").stop().animate({height:'100px'},{queue:false, duration:600, easing: 'linear'}).addClass('header-down'); 
    }
    else{
        $(".box_header").stop().animate({height:'30px'},{queue:false, duration:600, easing: 'linear'}).removeClass('header-down');
    }});
});


 $("#btn2").each(function(){
    $(this).on("click", function(){if(!$(".box_header").hasClass('header-down')){
        $("#box2").stop().animate({height:'100px'},{queue:false, duration:600, easing: 'linear'}).addClass('header-down'); 
    }
    else{
        $(".box_header").stop().animate({height:'30px'},{queue:false, duration:600, easing: 'linear'}).removeClass('header-down');
    }});
});

$(document).click(function() {
    if($(".box_header").hasClass('header-down')){
        $(".box_header").stop().animate({height:'30px'},{queue:false, duration:600, easing: 'linear'}).removeClass('header-down');
}
});

$(".box_header").click(function(e) {
    e.stopPropagation(); // This is the preferred method.
           // This should not be used unless you do not want
                         // any click events registering inside the div
});

Demo 演示版

The click event should be registered with the "ID" of the setting button and not with the css class, as it will apply to all the matched elements. click事件应该使用设置按钮的“ ID”注册,而不是使用css类注册,因为它将应用于所有匹配的元素。 Create a function passing that particular element and replace your $(".someBoxClass") with $(element). 创建一个传递该特定元素的函数,并将$(“。someBoxClass”)替换为$(element)。 For example, 例如,

$('#btn1').click(function(){animate(this)});

function animate(element){
$(element).stop().....
}

You should use ID instead of class. 您应该使用ID而不是类。

$("#box1").click(function () { 

$("#box2").click(function () { 

I think this is what you want: http://jsfiddle.net/MTcvu/ 我认为这就是您想要的: http : //jsfiddle.net/MTcvu/

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

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