【问题标题】:'return false' statement outside function error函数错误外的'return false'语句
【发布时间】:2016-01-07 09:17:29
【问题描述】:

我一直在尝试将以下 Javascript 代码实现到 Wordpress 项目的头部。我在这里找到了原始方法:jQuery isotope query filtering results for url 但最后一个'return false;'不断抛出错误'return not in function'。诚然,我不得不更新脚本以使用 jQuery 而不是原始脚本中的 $'s 并且已经包含了 jquery.cookie.js,因为他们在他们的工作示例中拥有它(因为认为它很旧)但想知道是否有人可以建议return false 错误的解决方法,所以我可以让这个概念与 jQuery 一起使用?谢谢

<script>
// filter items when filter link is clicked
		jQuery(document).ready( function() {
		jQuery("#filters a").click(function(){
			var selector = jQuery(this).attr('data-filter');
		jQuery('#portfolio').isotope({ filter: selector });
			return false;
		});
		});
</script>
<script>
		// Set cookie based on filter selector
		jQuery("#cookiefilter a").click(function(){
			var selector = jQuery(this).attr('data-filter');
			$.cookie("listfilter", selector, { path: '/projects/' });
		});	
		if ( $.cookie("listfilter") ) {
			jQuery('#portfolio').isotope({ filter: $.cookie("listfilter") });
			$.cookie("listfilter", null, { path: '/projects/' });
			return false;
		}
		
</script>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<script>
 * jQuery Cookie plugin
 *
 * Copyright (c) 2010 Klaus Hartl (stilbuero.de)
 * Dual licensed under the MIT and GPL licenses:
 * http://www.opensource.org/licenses/mit-license.php
 * http://www.gnu.org/licenses/gpl.html
 *
 */
jQuery.cookie = function (key, value, options) {

    // key and at least value given, set cookie...
    if (arguments.length > 1 && String(value) !== "[object Object]") {
        options = jQuery.extend({}, options);

        if (value === null || value === undefined) {
            options.expires = -1;
        }

        if (typeof options.expires === 'number') {
            var days = options.expires, t = options.expires = new Date();
            t.setDate(t.getDate() + days);
        }

        value = String(value);

        return (document.cookie = [
            encodeURIComponent(key), '=',
            options.raw ? value : encodeURIComponent(value),
            options.expires ? '; expires=' + options.expires.toUTCString() : '', // use expires attribute, max-age is not supported by IE
            options.path ? '; path=' + options.path : '',
            options.domain ? '; domain=' + options.domain : '',
            options.secure ? '; secure' : ''
        ].join(''));
    }

    // key and possibly options given, get cookie...
    options = value || {};
    var result, decode = options.raw ? function (s) { return s; } : decodeURIComponent;
    return (result = new RegExp('(?:^|; )' + encodeURIComponent(key) + '=([^;]*)').exec(document.cookie)) ? decode(result[1]) : null;
};
</script>
</head>

【问题讨论】:

  • 嗯,错误是可以的。回报根本不应该在那里。为什么需要它?
  • 您不需要在帖子中包含 html、head 或 script 标签。
  • 我不知道他们最初为什么使用它。我只是逐字复制了 JS,然后必须将 $ 更改为 jQuery 才能使其正常工作。我认为返回与杀死生成的 cookie 有关?
  • ..如果我删除最后一个 return false 我会得到一个错误: $ is undefined 'if ( $.cookie("listfilter") ) {'

标签: javascript php wordpress jquery-isotope jquery-cookie


【解决方案1】:

错误是正确的。您的“return false”发生在函数之外,这是不允许的。

<script>
    // Set cookie based on filter selector
    jQuery("#cookiefilter a").click(function(){
        var selector = jQuery(this).attr('data-filter');
        $.cookie("listfilter", selector, { path: '/projects/' });
    });    /****END OF FUNCTION****/

function() 到此结束。从这里开始,你在一个函数之外:

    if ( $.cookie("listfilter") ) {
        jQuery('#portfolio').isotope({ filter: $.cookie("listfilter") });
        $.cookie("listfilter", null, { path: '/projects/' });
        return false;
    }

也许你错误地提前结束了函数。
如果您将标记为 END OF FUNCTION 的行移到该

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-06-25
    • 1970-01-01
    • 1970-01-01
    • 2014-10-28
    • 2015-12-04
    • 2013-12-22
    相关资源
    最近更新 更多