Javascript 在JSLint错误中验证jQuery
刚遇到JSLint,所以决定让我的JS通过它,但有很多错误,但不确定它们是否坏。我尝试了一些在网上找到的方法来摆脱它们,但似乎没有做出任何改变 JS代码Javascript 在JSLint错误中验证jQuery,javascript,jquery,Javascript,Jquery,刚遇到JSLint,所以决定让我的JS通过它,但有很多错误,但不确定它们是否坏。我尝试了一些在网上找到的方法来摆脱它们,但似乎没有做出任何改变 JS代码 $( document ).ready(function() { !function (d, s, id) { var js, fjs = d.getElementsByTagName(s)[0], p = /^http:/.test(d.location) ? 'http' : 'https'; if (!d.
$( document ).ready(function() {
!function (d, s, id) {
var js, fjs = d.getElementsByTagName(s)[0],
p = /^http:/.test(d.location) ? 'http' : 'https';
if (!d.getElementById(id)) {
js = d.createElement(s);
js.id = id;
js.src = p + "://platform.twitter.com/widgets.js";
fjs.parentNode.insertBefore(js, fjs);
}
}(document, "script", "twitter-wjs");
$('div.icon').click(function () {
$('input#search').focus();
});
function search() {
var query_value = $('input#search').val();
$('b#search-string').html(query_value);
if (query_value !== '') {
$.ajax({
type: "POST",
url: "../_Scripts/search.php",
data: {
query: query_value
},
cache: false,
success: function (html) {
$("#results").html(html);
}
});
}
return false;
}
$("input#search").on("keyup", function (e) {
clearTimeout($.data(this, 'timer'));
var search_string = $(this).val();
if (search_string == '') {
$("#results").fadeOut();
$('h4#results-text').fadeOut();
} else {
$("#results").fadeIn();
$('h4#results-text').fadeIn();
$(this).data('timer', setTimeout(search, 100));
};
});
$('#pmForm').on('submit', function (e) {
e.preventDefault();
$('input[type=submit]', this).attr('disabled', 'disabled');
var pmSubject = $("#pmSubject", this).val();
var pmTextArea = $("#pmTextArea", this).val();
var url = "../_Scripts/private_msg_parse.php";
if (!pmSubject) {
$('input[type=submit]', this).removeAttr('disabled');
$("#jqueryReply").html('<img src="../_Images/round_error.png" alt="Error" width="31" height="30" /> Please type a subject.').show().fadeOut(6000);
return false;
} else if (!pmTextArea) {
$('input[type=submit]', this).removeAttr('disabled');
$("#jqueryReply").html('<img src="../_Images/round_error.png" alt="Error" width="31" height="30" /> Please type in your message.').show().fadeOut(6000);
return false;
} else {
$("#pmFormProcessGif").show();
$.post(url, $('#pmForm').serialize(), function (data) {
$("#jqueryReply").html(data).show().fadeOut(10000);
$("#pmTextArea").val('');
$("#pmSubject").val('');
$('input[type=submit]', this).removeAttr('disabled');
$("#pmFormProcessGif").hide();
});
}
});
$('#newblog').on('submit', function (e) {
e.preventDefault();
$('input[type=submit]', this).attr('disabled', 'disabled');
var blogTitle = $("#blogtitle").val();
var blogText = CKEDITOR.instances['blogbody'].getData();
var url = "../_Scripts/post-blog.php";
if (!blogTitle) {
$('input[type=submit]', this).removeAttr('disabled');
$("#jqueryReply").html('<img src="../_Images/round_error.png" alt="Error" width="31" height="30" /> Please type a Title.').show().fadeOut(6000);
return false;
} else if (!blogText) {
$('input[type=submit]', this).removeAttr('disabled');
$("#jqueryReply").html('<img src="../_Images/round_error.png" alt="Error" width="31" height="30" /> Please type in your Blog.').show().fadeOut(6000);
return false;
} else {
$("#blogFormProcessGif").show();
for (instance in CKEDITOR.instances) {
CKEDITOR.instances['blogbody'].updateElement();
}
$.post(url, $('#newblog').serialize(), function (data) {
$("#jqueryReply").html(data).show().fadeOut(10000);
$("#blogtitle").val('');
CKEDITOR.instances['blogbody'].setData('');
$("#blogFormProcessGif").hide();
location.reload();
});
}
});
function _(x) {
return document.getElementById(x);
};
function report(id, uid) {
var url = "../_Scripts/report.php";
$.post(url, {
blogid: id,
userid: uid
}, function (data) {
alert(data);
});
};
function toggleElement(x) {
var x = _(x);
if (x.style.display == 'block') {
x.style.display = 'none';
} else {
x.style.display = 'block';
}
};
var friendRequestURL = "../_Scripts/request_as_friend.php";
function addAsFriend(a, b) {
$("#add_friend_loader").show();
$.post(friendRequestURL, {
request: "requestFriendship",
mem1: a,
mem2: b
}, function (data) {
$("#jqueryReply").html(data).show().fadeOut(6000);
});
};
function acceptFriendRequest(x) {
$.post(friendRequestURL, {
request: "acceptFriend",
reqID: x
}, function (data) {
$("#frequestreply").html(data).show();
});
};
function denyFriendRequest(x) {
$.post(friendRequestURL, {
request: "denyFriend",
reqID: x
}, function (data) {
$("#frequestreply").html(data).show();
});
};
function removeAsFriend(a, b) {
$("#remove_friend_loader").show();
$.post(friendRequestURL, {
request: "removeFriendship",
mem1: a,
mem2: b
}, function (data) {
$("#jqueryReply").html(data).show().fadeOut(6000);
location.reload();
});
};
});
var date = new Date().getFullYear();
$('#year_' + date).children('ul').addClass('active').children('li').slideDown();
$('#' + date).children('img').attr("src", "../_Images/arrow-down.gif");
$('li:not(.recent)').click(function (e) {
if ($(this).find('>ul').hasClass('active')) {
$(this).children('ul').removeClass('active').children('li').slideUp();
$(this).children('img').attr("src", "../_Images/arrow.gif");
e.stopPropagation();
} else {
$(this).children('ul').addClass('active').children('li').slideDown();
$(this).children('img').attr("src", "../_Images/arrow-down.gif");
e.stopPropagation();
}
});
有谁能解释一下这些错误,告诉我它们是否会影响我的未来?目前,代码运行良好
谢谢JSLint检查代码语法,包括空格,而不仅仅是代码错误 由于JavaScript的性质,诸如
预期的“==”和相反看到的“==”之类的错误非常严重。在JavaScript中,==
表示比较值,而==
表示比较值和类型。以下是几个例子:
1 == '1' // Evaluates to true because it doesn't check for type
1 === '1' // Evaluates to false because it does check for type (they are
// different types)
因此,最好使用==
而不是=
空白错误/警告,例如
Unexpected space between '(' and 'document'.
及
它们没有那么严重,可能不会对代码的实际性能造成任何问题,但是,它们将有助于提高代码的可读性和清晰度。我建议使用JSLint格式/空白规则,但如果您有自己的有效JavaScript代码格式样式,就不用太担心了
JSLint是编写JavaSript代码的标准,而不是标准。也就是说,JSLint是一个非常好的标准。如果您不关心空格,那么JSHint是JSLint的一个很好的替代品。JSHint是JSLint的一个分支,但在不太重要的地方,即空格,它并没有那么严格
正如上面的评论所建议的,我建议集中精力学习如何编写好的、干净的、可读的JavaScript代码。在大多数情况下,如果代码编写良好且可读,JSLint/JSHint不会发现太多错误。关于制作好JavaScript代码的更多信息,我建议查看JSLint的发明者Douglas Crockford。您启用/禁用了哪些选项?您是否将$
和jQuery
列为“预定义全局变量?”JSLint是一个相当严格的验证器。默认情况下,is只假设核心ECMAScript,并且几乎没有定义任何其他内容。你必须告诉它,代码是为浏览器设计的,因此它需要引用文档
,窗口
,等等,并且你在这个脚本之前包含了jQuery。你的代码有点凌乱,我相信JSHint/JSLint,直到你掌握了JSHint/JSLint的编码技巧,然后尝试不同的选项以适应您的特定风格。但是它给你的所有警告都是要修正的;额外的分号,使用点符号,重复变量,使用==
,等等。其中许多看起来像是空格错误,请尝试打开“草率空白”,我想这就是所谓的。感谢您花时间解释。非常感谢并乐于助人:)
Unexpected space between '(' and 'document'.
Expected '}' at column 9, not column 5.