'返回false'声明外部功能错误

时间:2015-10-09 12:13:25

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

我一直在尝试将以下Javascript代码实现到Wordpress项目的主管中。我在这里找到了原始方法:jQuery isotope query filtering results for url但最后一个'返回false;'不断抛出错误'不返回功能'。不可否认,我必须更新脚本以使用jQuery而不是原始脚本中的$& s;已经包含了jquery.cookie.js,因为他们已经在他们的工作示例中使用它(因为它认为它已经老了)但是想知道是否有人可以为返回错误错误提出一个解决方法,以便我可以使用这个概念来使用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>
&#13;
<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>
&#13;
&#13;
&#13;

1 个答案:

答案 0 :(得分:2)

错误是正确的。你的&#34;返回false&#34;发生在函数外部,这是不允许的。

<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****/

函数()在这里结束。从这里开始,你就在一个功能之外:

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

也许你错误地提前结束了这个功能。
如果将标记为END OF FUNCTION的行移到该&lt; script&gt;的末尾,则应修复错误。

相关问题