It is easy to encounter various strange bugs when writing code. It is recommended to use Fundebug for online bug monitoring in real time.
The input is placed in the a tag and click cannot get the cursor of the input (in the IE environment)
Double-click to get the focus, there are currently solutions:
Do not add href attribute to a tag;
Don't put a tag on the outside.
hide the cursor of the input tag
Project requirements: input value json loaded, read-only + cursor hidden, general solution has other label simulation, but can not change input,
so the solution is to add the following two attributes to input:
//只读
readonly="readonly"
//隐藏光标
unselectable="on"
return private array
Returns a copy of the array so changes don't affect the original array, just a copy
var array = (function () {
var days = ['Mo', 'Tu', 'We', 'Th', 'Fr', 'Sa', 'Su'];
return {
getDays: function () {
return days.slice();
}
}
})()
extension of jquery selector
//jQuery contains 选择器,对Contains查找的内容不区分大小写
jQuery.expr[':'].Contains = function (a, i, m) {
return jQuery(a).text().toUpperCase()
.indexOf(m[3].toUpperCase()) >= 0;
};
example
<div>john</div>
<div>John</div>
<div>hey hey JOHN hey hey</div>
$('div:Contains("john")') //会选择到两个div
When a variable is declared, extending its properties does not change the original data type
var a = 'foo';
a[1] = 'O';
console.log(0.1+0.2==0.3||a); //'foo'
Closures are nested definitions of functions, not nested invocations of functions
function foo(){
console.log(a);
}
function bar () {
var a = 3;
foo();
}
var a = 2;
bar(); //2
DOMContentLoaded is compatible with versions below IE9
//jQuery的实现
// Mozilla, Opera and webkit nightlies currently support this event
if ( document.addEventListener ) {
// Use the handy event callback
document.addEventListener( "DOMContentLoaded", DOMContentLoaded, false );
// A fallback to window.onload, that will always work
window.addEventListener( "load", jQuery.ready, false );
// If IE event model is used
} else if ( document.attachEvent ) {
// ensure firing before onload,
// maybe late but safe also for iframes
document.attachEvent( "onreadystatechange", DOMContentLoaded );
// A fallback to window.onload, that will always work
window.attachEvent( "onload", jQuery.ready );
// If IE and not a frame
// continually check to see if the document is ready
var toplevel = false;
try {
toplevel = window.frameElement == null;
} catch(e) {}
if ( document.documentElement.doScroll && toplevel ) {
doScrollCheck();
}
}
//函数DOMContentLoaded的赋值
if ( document.addEventListener ) {
DOMContentLoaded = function() {
document.removeEventListener( "DOMContentLoaded", DOMContentLoaded, false );
jQuery.ready();
};
} else if ( document.attachEvent ) {
DOMContentLoaded = function() {
// Make sure body exists, at least, in case IE gets a little overzealous (ticket #5443).
if ( document.readyState === "complete" ) {
document.detachEvent( "onreadystatechange", DOMContentLoaded );
jQuery.ready();
}
};
}
// The DOM ready check for Internet Explorer
function doScrollCheck() {
if ( jQuery.isReady ) {
return;
}
try {
// If IE is used, use the trick by Diego Perini
// http://javascript.nwbox.com/IEContentLoaded/
document.documentElement.doScroll("left");
} catch(e) {
setTimeout( doScrollCheck, 1 );
return;
}
// and execute any waiting functions
jQuery.ready();
}
//网友的实现
// @win window reference
// @fn function reference
function contentLoaded(win, fn) {
var done = false, top = true,
doc = win.document,
root = doc.documentElement,
modern = doc.addEventListener,
add = modern ? 'addEventListener' : 'attachEvent',
rem = modern ? 'removeEventListener' : 'detachEvent',
pre = modern ? '' : 'on',
init = function(e) {
if (e.type == 'readystatechange' && doc.readyState != 'complete') {
return;
}
//load事件在win上,移除事件监听(readystatechange, DOMContentLoaded, load)
(e.type == 'load' ? win : doc)[rem](pre + e.type, init, false);
//保证fn只执行一次
if (!done && (done = true)) fn.call(win, e || e.type);
},
poll = function() {
try { root.doScroll('left'); } catch(e) { setTimeout(poll, 50); return; }
init('poll');
};
if (doc.readyState == 'complete') fn.call(win, 'lazy');
else {
if (!modern && root.doScroll) {
try { top = !win.frameElement; } catch(e) { }
if (top) poll();
}
doc[add](pre + 'DOMContentLoaded', init, false); //触发时,doc.readyState为'interactive'
doc[add](pre + 'readystatechange', init, false); //会触发两次,readystatechange先触发,再触发DOMContentLoaded, 最后才是load
win[add](pre + 'load', init, false);
}
}
The difference between document.querySelectorAll and getElementsByTagName
//html代码
<ul>
<li>a</li>
<li>b</li>
<li>c</li>
</ul>
//script代码
var ul = document.querySelector('ul');
var li = ul.querySelectorAll('li'),
tagLi = ul.getElementsByTagName('li'); //动态取值
ul.appendChild(document.createElement('li'));
console.log(li.length); //3 li.toString()为[object NodeList]
console.log(tagLi.length); //4 tagLi.toString()为[object HTMLCollection]