最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - JSLint - Undeclared 'jQuery' warning - Stack Overflow

programmeradmin4浏览0评论

I am trying to make the following jQuery code JSLint pliant.

(function ($) {
   "use strict";
    $('.addition').click(function () {
        $('.textbox:last,.addition:last,.subtraction:last,.replace:last').clone(true).appendTo('.replace:last');
        $('.textbox:last').val("");
        $('.addition:not(:last),.subtraction:not(:last)').attr('disabled','true');
    });

    $('.subtraction').click(function () {

        if($('.replace').length === 1) {
            $('.subtraction').click(function (event) {
                event.preventDefault();
            });
        } else {
            $('.textbox:last,.addition:last,.subtraction:last,.replace:last').remove();
            $('.addition:last,.subtraction:last').removeAttr('disabled');
        }   
    });     
}(jQuery));

My code is working fine. I am just trying to clear the JSLint warnings. I do not understand why JSLint is giving me an undeclared 'jQuery' warning on line }(jQuery)); (last line).

UPDATE
Declaring jQuery as a global variable has cleared the warning.

I am trying to make the following jQuery code JSLint pliant.

(function ($) {
   "use strict";
    $('.addition').click(function () {
        $('.textbox:last,.addition:last,.subtraction:last,.replace:last').clone(true).appendTo('.replace:last');
        $('.textbox:last').val("");
        $('.addition:not(:last),.subtraction:not(:last)').attr('disabled','true');
    });

    $('.subtraction').click(function () {

        if($('.replace').length === 1) {
            $('.subtraction').click(function (event) {
                event.preventDefault();
            });
        } else {
            $('.textbox:last,.addition:last,.subtraction:last,.replace:last').remove();
            $('.addition:last,.subtraction:last').removeAttr('disabled');
        }   
    });     
}(jQuery));

My code is working fine. I am just trying to clear the JSLint warnings. I do not understand why JSLint is giving me an undeclared 'jQuery' warning on line }(jQuery)); (last line).

UPDATE
Declaring jQuery as a global variable has cleared the warning.

Share Improve this question edited May 30, 2015 at 6:14 user3421311 asked May 30, 2015 at 5:59 user3421311user3421311 1712 silver badges10 bronze badges 1
  • possible duplicate of How can I use JSLint for a segment of code which depends on JQuery? – Alexander O'Mara Commented May 30, 2015 at 6:03
Add a ment  | 

2 Answers 2

Reset to default 11

If you're using jslint., you'll need to add jQuery to Global variables under options

If you're using jslint., you need to replace 1st line of your code. you need to add "global jQuery" as a ment line. See here..

replace: (function ($) {

by using: /*global jQuery */
jQuery(function ($) { .....

All the Best.
Puspendu Mondal

发布评论

评论列表(0)

  1. 暂无评论