& # 39;返回错误# 39;函数外的语句错误
'return false' statement outside function error
我一直在尝试将以下Javascript代码实现到Wordpress项目的头部。我在这里找到了原始方法:jQuery同位素查询过滤url的结果,但最后的"返回false;"一直抛出错误"返回不在功能中"。诚然,我不得不更新脚本,使用jQuery代替原来脚本中的$ & &;已经包括jQuery .cookie.js,因为他们在他们的工作示例(因为认为它是旧的),但想知道是否有人可以建议一个解决方案的返回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>
错误是正确的。你的"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****/
函数()在这里结束。从这里开始,您就在函数之外了:
if ( $.cookie("listfilter") ) {
jQuery('#portfolio').isotope({ filter: $.cookie("listfilter") });
$.cookie("listfilter", null, { path: '/projects/' });
return false;
}
可能您错误地提前结束了函数。
如果您将标记为END OF FUNCTION的行移动到该
相关文章:
- 将错误与if语句混淆
- 如何知道javascript for语句中的所有结果是否都是错误的
- MVC Jquery-按钮点击-获取if语句错误
- 由 if / else 语句中的无效语法导致的语法错误
- Javascript else-if语句错误
- 阻止表单提交 javascript 为什么有一个明显的错误语句返回 true
- 筛选器后的行计数错误(使用if语句)
- Simple Javascript if语句中存在语法错误
- Null记录错误地触发Else语句
- Javascript if else语句OR条件错误
- 如果语句返回错误答案
- 防止 if 语句中的错误“未定义”
- 使用 If else if else 语句得到错误
- ES6 大箭头符号函数给出语法错误:缺失;在语句之前
- 非法返回语句错误
- 为什么我的 if 语句出现语法错误
- 为什么以下语句执行没有错误
- 我的“if”语句中包含中断的错误(wrox ch 3 练习 4)
- 为什么在调用丢失的函数时会捕获错误,而不是在错误语句中捕获错误
- jQuery无线电检查命中错误语句