简体   繁体   English

使用jQuery显示/隐藏文章

[英]Show/Hide article with jquery

I'd like to create a menu that show's and hide's articles. 我想创建一个显示和隐藏文章的菜单。 Someone gave me this code, but for some reason its not working. 有人给了我这段代码,但是由于某种原因它不起作用。 It says "toon" is an undefined function in de element inspector. 它说“卡通”是de element inspector中未定义的功能。

What am I missing here? 我在这里想念什么?

$ (document). ready(function(){
     $ ('article').hide();
     var zichtbaar = $('article').first();
     zichtbaar.show();
     var toon = function(artikel) {
     zichtbaar.hide();
     zichtbaar = artikel;
     zichtbaar.show();
     };

     $('#menu a').click(function(event) {
     var ref = $(this).attr('href');
     toon(ref);
     event.preventDefault();
     });
});

I see problem, your function toon has to look like this: 我看到了问题,您的功能卡通必须如下所示:

var toon = function(artikel) {
   zichtbaar.hide();
   zichtbaar = $(artikel);
   zichtbaar.show();
};

You are sending through variable a #articleID, but you have to assign a jQuery object to use .show() method, so you have to wrap your #articleID into $() 您正在通过变量发送#articleID,但必须分配一个jQuery对象以使用.show()方法,因此必须将#articleID包装为$()

Is that you are miss '#', I am assume that you "article" is a HTML ID 是您错过了“#”吗,我假设您的“文章”是HTML ID

$ (document). ready(function(){
         $ ('#article').hide();//Add a #
         var zichtbaar = $('#article').first();////Add a #
         zichtbaar.show();
         var toon = function(artikel) {
            zichtbaar.hide();
            zichtbaar = artikel;
            zichtbaar.show();
         };

         $('#menu a').click(function(event) {
            var ref = $(this).attr('href');
            toon(ref);
            event.preventDefault();
         });
    });

You should add a jsfiddle to your code. 您应该在代码中添加一个jsfiddle Then we can better debug it for you. 然后,我们可以更好地为您调试它。 You have 2 problems in your code. 您的代码中有2个问题。 (Edit: I apologize I didn't know that article was a HTML5 tag. Tech moves fast.) (编辑:很抱歉,我不知道该文章是HTML5标签。技术发展迅速。)

Problem 1: .show(); 问题1:.show(); of a child element reveals the entire parent element 子元素的元素显示整个父元素

$("#article").hide();
var zichtbaar = $("#article").first();
zichtbaar.show(); //when a child element is shown, the parent element is shown as well

Fiddle . 小提琴

You .show();-ing your .first(); 您.show();-您的.first(); #article element is essentially revealing the parent element #article (hence revealing the rest of the child elements). #article元素本质上是在显示父元素#article(因此揭示了其余的子元素)。 What you could do is hide the child elements instead like this: 您可以做的是像这样隐藏子元素:

$(document).ready(function(){
    $("#articleholder article:not(#1)").hide(); //smart CSS
    //other codes below
});

Problem 2: Your function is undefined because of JavaScript hoisting 问题2:由于JavaScript吊起,您的函数未定义

var toon = function(artikel) {
     zichtbaar.hide();
     zichtbaar = artikel; //all var declarations within functions like these hoist
     zichtbaar.show();
};

Your code above won't work because of javascript hoisting . 您的上述代码由于javascript吊起而无法正常工作。

Due to hoisting, JavaScript engines will render your function as: 由于提升,JavaScript引擎会将您的函数呈现为:

var toon = function(artikel) {
    var zichtbaar; //all variables in functions are hoisted to the top and declared like this
    zichtbaar.hide(); //at this point, zichtbaar is undefined. Hence the debugger says your function is undefined.
    zichtbaar = artikel; //all var declarations within functions like these hoist
    zichtbaar.show();
}

So instead you could do this: 因此,您可以这样做:

$(document).ready(function(){
    $("#articleholder article:not(#1)").hide(); //smart CSS
    var toon = function(artikel) {
        $("#articleholder article").hide(); //we will hide everything so that future clicks will work as well
        artikel.show(); //greatly simplified.
    };

    $("#menu a").click(function(event){
        var ref = $(this).attr('href');
        toon($(ref)); //just plug in the jQuery object as the argument
    });
});

Here is your complete solution: Working fiddle . 这是您的完整解决方案: 工作提琴

In your toon() function the artikel parameter is a string you cannot do show() on a string. 在您的toon()函数中, artikel参数是一个字符串,您无法在字符串上执行show() Also the function will never hide any element you should use toggle() to switch between visible/hidden states. 而且该函数将永远不会隐藏您应使用toggle()在可见/隐藏状态之间toggle()任何元素。

$(document).ready(function () {
    $('article').hide().first().show();//you can use chaining
    var toon = function (artikel) {
        $(artikel).toggle();
    };

    $('#menu a').click(function (event) {
        var ref = $(this).attr('href');
        toon(ref);
        event.preventDefault();
    });
});

WORKING DEMO 工作演示

Note: Nothing wrong with $('article') because <article> is an html5 tag. 注意: $('article')因为<article>是html5标签。

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

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