var a = [1,2,3,4];
$.each(a, function ()
{
console.log( this );
});
// !
better this way:for ( var i = 0, l = a.length; i < l; i++)
{
console.log(a[i]);
}
In the usual approach, you do not spend time on calls to callback functions and unnecessary checks to stop the cycle. Another example:var b = {c: 1, d: 2, e: 3};
$.each(b, function (k)
{
console.log(k, this );
});
//
better this way:for ( var k in b)
{
if ( b.hasOwnProperty(k) )
{
console.log(k, b[k]);
}
}
$.fn.inputChange = function (callback)
{
return this .bind(
{
mousedown: callback,
keyup: callback,
blur: callback
});
};
and it works like this:var submit = $( "#submit" );
// 1
$( "#text" ).inputChange( function ()
{
submit[0].disabled = this .value.length === 0;
});
// 2
$( "#text" ).inputChange( function ()
{
if ( this .value.length === 0)
{
submit.attr( "disabled" , "disabled" );
}
else
{
submit.removeAttr( "disabled" );
}
});
However, both options are far from ideal: 1 may fall with an error if suddenly there is no element inside submit, 2 is too cumbersome.
$.fn.disable = function ( bool )
{
return this .each( function ()
{
this .disabled = bool ;
});
};
and this plugin works in conjunction with $ .fn.inputChange like this:$( "#text" ).inputChange( function ()
{
submit.disable( this .value.length === 0);
});
However, there was a problem that no event specified in inputChange can handle pasting from the clipboard. Googling, came to a decision:$.fn.paste = function (listener)
{
if ($.browser.msie)
{
return this .bind( "paste" , function ( event )
{
var that = this ;
setTimeout(
function ()
{
listener.apply(that, [ event ]);
},
.001
);
});
}
else
{
return this .bind( "input" , function ( event )
{
listener.apply( this , [ event ]);
});
}
};
and the inputChange plugin takes the following form:$.fn.inputChange = function (callback)
{
return this
.paste(callback)
.bind(
{
keyup: callback,
blur: callback
});
};
So “brick by brick” we have assembled the working functionality, adding additional plug-ins to the library, which will surely come in handy for us, and maybe you ...
function warning(text)
{
$( "<div id='alert' class='warning'></div>" )
.appendTo( $( document .body) )
.css(
{
"padding" : "10px" ,
"background-color" : "red" ,
"color" : "white"
})
.html(text)
.append(
$( "<button>OK</button>" ).attr( "title" , "Close me!" ).click( function ()
{
$( this ).parent().remove();
})
);
}
In many ways, the code is sucked from the finger, but only for demonstration purposes. Now we will try to rewrite the code in pure JS:function warning(text)
{
var div = document .createElement( "div" );
div.id = "alert" ;
div.className = "warning" ;
div.style.padding = "10px" ;
div.style.backgroundColor = "red" ;
div.style.color = "white" ;
div.innerHTML = text;
var button = document .createElement( "button" );
button.setAttribute( "title" , "Close me!" );
button.innerHTML = "OK" ;
$(button).click( function ()
{
div.parentNode.removeChild(div);
});
div.appendChild(button);
document .body.appendChild(div);
}
I deliberately added the event using jQuery, since jQuery well encapsulates the cross-browser calls to addEventListener / attachEvent, erasing the boundaries between browsers and for the creators of jQuery great respect. For the rest, the standard methods for working with the DOM are used.
Source: https://habr.com/ru/post/103296/