【问题标题】:Calling a cookie-jquery function in body?在正文中调用 cookie-jquery 函数?
【发布时间】:2014-01-23 16:12:57
【问题描述】:

我需要一些帮助,我从朋友那里得到了一个 cookie 接受的 jquery 代码,我们必须在丹麦使用它。

我的问题是我真的不知道如何在html中调用我体内的函数?

谁能帮忙,我在 jsFiddle 中找到了代码,应该很容易让你看一下。

HTML:

This is a test


<script>
    jQuery(document).load(_cookieAccept);
</script>

JS:

var _cookieAccept = {

    init: function () {

        if (this.hasAccepted()) {
            return;
        }

        //
        var bar = $j('<div>')
            .addClass('cookieAccept')
            .append($j('<span>').html('<img src="http://www.computerworld.dk/common/images/cookie_warning_1.gif" />Websitet anvender cookies til at huske dine indstillinger, statistik og at målrette annoncer. <a class="readMore" href="/privacy" target="_blank">Læs mere</a>'))
            .append($j('<a>').attr('href', 'javascript:void(0)').addClass('accept').html('OK').click(this.clickAccept))

        //
        bar.css({
            position: 'fixed',
            left: 0,
            bottom: 0,
            width: '100%',
            backgroundColor: 'rgba( 0, 0, 0, 0.7 )',
            color: '#ffffff',
            padding: '15px',
            fontSize: 16,
            lineHeight: '20px',
            fontFamily: 'Arial',
            zIndex: 10000,
            textAlign: 'left'
        });

        bar.find('a.readMore').css({
            color: '#ffffff',
            textDecoration: 'underline',
            fontWeight: 'normal'
        });

        bar.find('a.accept').css({
            float: 'right',
            marginRight: '50px',
            backgroundColor: '#ffffff',
            padding: '5px 20px',
            borderRadius: '4px',
            color: '#000000',
            fontSize: '14px'
        });

        bar.find('a.accept').hover(function () {
            $j(this).css('backgroundColor', '#7c94a0');
        }, function () {
            $j(this).css('backgroundColor', '#ffffff');
        });

        //
        $j('body').append(bar);
    },

    /**
     * 
     */
    hasAccepted: function () {
        return (document.cookie.indexOf('cookieAccept=Yes') !== -1);
    },

    /**
     * 
     */
    setAccept: function () {
        var expire = new Date();
        expire.setDate(expire.getDate() + (365 * 10));
        document.cookie = 'cookieAccept=Yes;expires=' + expire.toGMTString() + ';path=/';
    },

    /**
     * 
     */
    clickAccept: function () {

        _cookieAccept.setAccept();

        $j('div.cookieAccept').hide();
    }
};

$j(document).ready($j.proxy(_cookieAccept.init, _cookieAccept));

http://jsfiddle.net/k23MC/

【问题讨论】:

  • .load() 的参数应该是要调用的函数,而不是对象。

标签: javascript jquery cookies session-cookies


【解决方案1】:
<script type="text/javascript">
    <!-- Your code here -->
</script>

应该正常完成这项工作,即使在body标签内,也没关系。

如果控制台有任何语法错误,请使用 firebug 或类似工具检查。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-11-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-10-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多