简体   繁体   English

使用jQuery显示带有超链接的Div

[英]Reveal Div with Hyperlink using jQuery

I have a jQuery Panel that opens when clicking a button. 我有一个jQuery面板,当单击一个按钮时会打开。

However, I'd like a 2nd link at the bottom of the page which opens the same panel. 但是,我想要页面底部的第二个链接打开同一面板。

How do I achieve this? 我该如何实现?

Here is my JSFiddle & my Javascript: 这是我的JSFiddle和我的Javascript:

$(document).ready(function() {

    // Expand Panel
    $("#open").click(function(){
        $("div#panel").slideDown("slow");   
        $("#open").addClass("hidden");
         $("#close").removeClass("hidden");
    });     

    // Switch visiblility of the "Close Panel" button
    $("#close").click(function () {
        $("div#panel").slideUp("slow"); 
        $("#close").addClass("hidden");
        $("#open").removeClass("hidden");
    });

    });

Many thanks for any guidance. 非常感谢您的指导。

it may be so simple but if you trigger the click event of your button, it should make the effect which you need to achieve. 它可能很简单,但是如果触发按钮的click事件,它应该会产生所需的效果。

$("#myLink").click(function(){
    $("#open").click();
    });

Here is the updated fiddle: http://jsfiddle.net/4dkoke6w/2/ 这是更新的小提琴: http : //jsfiddle.net/4dkoke6w/2/

if this is not exactly what you want, let me know and I can tweak it a little bit per your requirement. 如果这不是您想要的,请告诉我,我可以根据您的要求进行一些调整。

Extend your selector: 扩展选择器:

// Expand Panel
$("#open, #link").click(function(){
    $("div#panel").slideDown("slow");   
    $("#open").addClass("hidden");
    $("#close").removeClass("hidden");
});     

Like this: 像这样:

$(document).ready(function() {

function showPanel(){
 $("div#panel").slideDown("slow");   
        $("#open").addClass("hidden");
         $("#close").removeClass("hidden");
}

// Expand Panel
$("#open,#second-button-you-talked-about").click(showPanel); 
//or    
$("#open").click(showPanel);
$("#second-button-you-talked-about").click(showPanel);

// Switch visiblility of the "Close Panel" button
$("#close").click(function () {
    $("div#panel").slideUp("slow"); 
    $("#close").addClass("hidden");
    $("#open").removeClass("hidden");
});

});

(you should also store your jQuery elements in variables) (您还应该将jQuery元素存储在变量中)

use class instead of id 使用类而不是id

$(".opendivs").click(function(){
    $("div#panel").slideDown("slow");   
    $("#open").addClass("hidden");
     $("#close").removeClass("hidden");
}); 

give the class opendivs to all the links you want to open the div with 将class opendivs分配给您要用来打开div的所有链接

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

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