javascript - 函数外部的“return false”语句错误

标签 javascript php wordpress jquery-isotope jquery-cookie

我一直在尝试将以下 Javascript 代码实现到 WordPress 项目的头部。我在这里找到了原始方法:jQuery isotope query filtering results for url但最后一个'return false;'不断抛出错误“返回不在函数中”。不可否认,我必须更新脚本以使用 jQuery 而不是原始脚本中的 $,并包含 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>

最佳答案

错误是正确的。您的“返回 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 的行移至该 <script> 的末尾,则应修复该错误。

关于javascript - 函数外部的“return false”语句错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33038008/

相关文章:

php - 如何从 WP_Post 对象获取标题

html - 插入表单到wordpress页面

javascript - 如何使用 JavaScript 或 Ajax 在下拉菜单中显示每个选项的工具提示?

php - SQL - 连接两个表

php - WordPress 按距离和特色帖子查询排序

php - 如何根据服务时间禁用时段?

javascript - Ajax 错误: Failed to load resource: the server responded with a status of 500 (Internal Server Error)

JavaScript - 传单,添加一堆标记

javascript - 使用 JS 无法实现多个表单操作?

javascript - AngularJS 动态输入值绑定(bind)到数组模型