專案中遇到的bug(web前端-持續更新)
阿新 • • 發佈:2019-02-07
專案中遇到的常見bug,及時整理。
input放在a標籤裡面單擊不能獲取input的游標(IE環境下)
雙擊才可以獲得焦點,目前有的解決方案:
不要給a標籤新增href屬性;
不要在外面套上a標籤。
隱藏input標籤的游標
專案需求:input值json載入,只讀+游標隱藏,通用的解決方案有其他標籤模擬,但是不能改input
所以解決方案為給input加下面這兩個屬性:
//只讀
readonly="readonly"
//隱藏游標
unselectable="on"
返回私有陣列
返回陣列的一個副本,這樣改動就不會影響原陣列,只是副本而已
var array = (function () {
var days = ['Mo', 'Tu', 'We', 'Th', 'Fr', 'Sa', 'Su'];
return {
getDays: function () {
return days.slice();
}
}
})()
jquery選擇器的擴充套件
//jQuery contains 選擇器,對Contains查詢的內容不區分大小寫
jQuery.expr[':'].Contains = function (a, i, m) {
return jQuery(a).text().toUpperCase()
.indexOf(m[3].toUpperCase()) >= 0;
};
例子
<div>john</div>
<div>John</div>
<div>hey hey JOHN hey hey</div>
$('div:Contains("john")') //會選擇到兩個div
當一個變數被聲明後,擴充其屬性並不會改變原資料型別
var a = 'foo';
a[1] = 'O' ;
console.log(0.1+0.2==0.3||a); //'foo'
閉包是函式的巢狀定義,而不是函式的巢狀呼叫
function foo(){
console.log(a);
}
function bar () {
var a = 3;
foo();
}
var a = 2;
bar(); //2
DOMContentLoaded相容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);
}
}
document.querySelectorAll和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]