无法从单独的文件访问JQuery或Foundation函数?

时间:2015-03-31 22:42:22

标签: javascript jquery modal-dialog zurb-foundation

基本上,问题是: 我有一些代码可以为页面上的每个按钮添加一个事件监听器。如果未设置登录cookie,则代码使用Foundation提供的函数触发Foundation模式。如果设置了cookie并且用户已登录,则用户将被定向到他们想要的页面。

问题是,即使基础文件包含在同一页面中,我似乎无法从我的文件中激活基础代码;其他一切都在运作。这是怎么回事?这是代码:

// Anonymous function wrapper
(function () {
    // getting an array of all buttons on the page, based on a button class
    var button = document.getElementsByClassName('button');

    // main function to run after ternary operator check (found at bottom)
    function run () {
        // cookieCheck() returns true/false depending on whether 
        // the cookie is set
        var cookie = cookieCheck(),
            modal = document.getElementById('loginModal'),
            i, j = button.length;

        // looping through each button, applying an even listener
        for (i = 0; i < j; ++i) {
            (function (i) {
                button[i].addEventListener('click', function (e) {
                    var link = e.target.getAttribute('target');
                    if (cookie === false) {
                        // Firing foundation function. PROBLEM IS HERE!!!
                        $('#myModal').foundation('reveal', 'open');
                    } else {
                        // if cookie is set, direct user to proper page
                        e.target.href = link;
                    }
                }, false);
            }(i));
        }
    }

    // if buttons exist on the page, the function runs.
    // this prevents errors being thrown in the console on pages with no buttons
    button ? run() : false;
}());

HTML(我没有写这个部分,或者PHP,我刚刚在这里写了javascript):

<?php wp_footer(); ?>

    <div id="loginModal" class="reveal-modal small" data-reveal>
        <?php gravity_form( 2, true, true, false, '', false ); ?>
    <a class="close-reveal-modal">&#215;</a></div>

    <script>
        (function($) {
            $(document).foundation();
        })(jQuery);
    </script>

    <!-- This is the script in question /-->
    <script src="/static/themes/RWv3/js/custom.js"></script>
    <?php if (is_front_page()) { ?>
        <script src="/static/themes/RWv3/js/vendor/main.js"></script>
    <?php } ?>
</body>

为什么会这样?

感谢

0 个答案:

没有答案