简体   繁体   English

立即调用页面默认值的函数表达式

[英]Immediately Invoked Function Expression For Page Defaults

I've tried to setup a very "vanilla" approach to this but cannot get the result. 我试图为此设置一个非常“香草”的方法,但无法获得结果。

I'm trying to reach into the DOM and the associated div styles using JS and effectively change the "display" property of the CSS. 我正在尝试使用JS来了解DOM和相关的div样式,并有效地更改CSS的“ display”属性。

The JS is error free but the CSS doesn't change. JS没有错误,但CSS不变。

 (function() { var singleCard = document.getElementById('card-container'); var manyCard = document.getElementById('card-container-many'); var allCard = document.getElementById('card-container') && document.getElementById('card-container-many'); var singleCardCss = document.querySelector('#card-container'); var manyCardCss = document.querySelector('#card-container-many'); var allCardCss = document.querySelector('#card-container') && document.querySelector('#card-container-many'); if (singleCardCss.display && manyCardCss.display === 'none') { allCardCss.display = 'block'; } else { allCardCss.display = 'none'; } }()); 
 #card-container { position: relative; display: none; width: 280px; height: 310px; background-size: 640px 360px; background-repeat: no-repeat; border: 1px solid #222; border-radius: 10px; margin: 10px; cursor: pointer; } #card-container-many { position: relative; display: none; width: 280px; height: 310px; background-size: 640px 360px; background-repeat: no-repeat; border: 1px solid #222; border-radius: 10px; margin: 10px; cursor: pointer; } 
 <div class="container-fluid text-center"> <div id="card-container"></div> </div> <div class="container-fluid text-center"> <div id="card-container-many"></div> </div> 

Your error is very common. 您的错误很常见。 You have to remove the last ) after your function. 您必须删除函数后的最后一个) You close your IIFE after calling it. 您调用它后关闭IIFE。 You can try but your function will be never call! 您可以尝试,但是您的函数将永远不会被调用! You also can try to delete your variable allCardCss and allCard. 您也可以尝试删除变量allCardCss和allCard。 I do not understand why do you initialize them with &&. 我不明白您为什么用&&初始化它们。

Replace: 更换:

(function() {
    var singleCard = document.getElementById('card-container');
    var manyCard = document.getElementById('card-container-many');
    var allCard = document.getElementById('card-container') && document.getElementById('card-container-many');

    var singleCardCss = document.querySelector('#card-container');
    var manyCardCss = document.querySelector('#card-container-many');
    var allCardCss = document.querySelector('#card-container') && document.querySelector('#card-container-many');

    if (singleCardCss.display && manyCardCss.display === 'none') {
        singleCardCss.display = 'block';

    } else {
        allCardCss.display = 'none';
    }
}());

By: 通过:

(function() {
    var singleCard = document.getElementById('card-container');
    var manyCard = document.getElementById('card-container-many');


    var singleCardCss = document.querySelector('#card-container');
    var manyCardCss = document.querySelector('#card-container-many');


    if (singleCardCss.display && manyCardCss.display === 'none') {
        singleCardCss.display = 'block';
        manyCardCss.display = 'block';
    } else {
        singleCardCss.display = 'none';
        manyCardCss.display = 'none';
    }
})();

The .style property is missing. .style属性丢失。 For example: 例如:

 allCardCss.style.display = 'block';

Also, the use of the AND operator is wrong I believe. 而且,我相信使用AND运算符是错误的。 It should be used in the if condition like so: 应该在if条件中使用,例如:

if (singleCardCss.style.display === "none" && manyCardCss.style.display === 'none') {...

Each side of the operand must be complete in a conditional even when it's a comparison between 2 objects ( singleCardCSS and manyCardCSS ) vs. the same condition ( "none" ). 即使是两个对象( singleCardCSSmanyCardCSS )与相同条件( "none" )之间的比较,操作数的每一边也必须在条件中完整。

I took a third look and saw that allCardCSS is wrong as well, it should be: 我看了一下第三眼,发现allCardCSS也是错误的,应该是:

var allCardCSS = document.querySelectorAll('div > div');

The result will be a NodeList of all divs that are a child of another div ( singleCardCSS and manyCardCSS ). 结果将是属于另一个div( singleCardCSSmanyCardCSS )的子singleCardCSS的所有div的NodeList。 This NodeList is an array-like object which you can do simple iterations upon in order to access the objects within. 这个NodeList是一个类似数组的对象,您可以对其进行简单的迭代以访问其中的对象。 Notice how the for loop goes through the NodeList allCardCss . 请注意for循环如何通过NodeList allCardCss

Finally on the last statement has been eliminated because the else isn't needed since they are already .style.display="none" . 最后,最后一条语句已被消除,因为不需要其他,因为它们已经是.style.display="none" The first statements have been eliminated as well because .getElementById('ID') is identical to querySelector('#ID'); 由于.getElementById('ID')querySelector('#ID');相同,因此也消除了第一条语句querySelector('#ID');

One last thing, I almost forgot about the parenthesis business: 最后一件事,我几乎忘记了括号业务:

Either of the following two patterns can be used to immediately invoke a function expression, utilizing the function's execution context to create "privacy." 以下两种模式中的任何一种都可以用于立即调用函数表达式,并利用函数的执行上下文来创建“隐私”。

(function(){ /* code */ }()); // Crockford recommends this one

(function(){ /* code */ })(); // But this one works just as well  

- Ben Alman - 本·阿尔曼

So you are ok. 所以你还好。 The point is that if you have a set of extra parenthesis at the end then that'll be interpreted by the browser as an E xpression F unction which causes an I mmediate I nvocation*. 问题的关键是,如果你在最后有一组额外的括号那么就可以通过浏览器作为一个E上的表达˚F油膏这导致 mmediate nvocation *解释。 The mention of privacy is referring to the IIFE with a closure which doesn't apply in your circumstance unless you make the latter part of the code into a function in which case you have a closure. 提到隐私是指带有闭包的IIFE,除非您将代码的后半部分变成函数,否则在您遇到闭包的情况下,闭包将不适用。 In your case it's not needed since you aren't passing any variables from the outside of your function. 在您的情况下,由于不需要从函数外部传递任何变量,因此不需要。

To those more knowledgeable. 给那些比较有知识的人。 If there's anything I've said that to the contrary or omitted, please leave a comment with your downvote. 如果我有相反的说法或忽略了任何内容,请留下您的反对意见。

*it's IIFE a little mixed up in order in sentence but you get the picture 😉 * IIFE的句子顺序有些混乱,但您会看到图片😉

Demo 演示版

 (function() { var singleCardCss = document.querySelector('#card-container'); var manyCardCss = document.querySelector('#card-container-many'); var allCardCss = document.querySelectorAll('div > div'); if (singleCardCss.style.display === "none" && manyCardCss.style.display === 'none') { for (let i = 0; i < allCardCSS.length; i++) { allCardCss[i].style.display = 'block'; } } }()); 
 #card-container { position: relative; display: none; width: 280px; height: 310px; background-size: 640px 360px; background-repeat: no-repeat; border: 1px solid #222; border-radius: 10px; margin: 10px; cursor: pointer; } #card-container-many { position: relative; display: none; width: 280px; height: 310px; background-size: 640px 360px; background-repeat: no-repeat; border: 1px solid #222; border-radius: 10px; margin: 10px; cursor: pointer; } 
 <div class="container-fluid text-center"> <div id="card-container"></div> </div> <div class="container-fluid text-center"> <div id="card-container-many"></div> </div> 

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

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