1、focus和blur事件不支援冒泡,但是它支援捕獲,所以在事件代理中我們可以使用捕獲,但是ie不支援捕獲,ie可以通過focusin和focusout這兩個專有事件來代替,它們是支援冒泡的
2、需要做頁面重新整理的時候推薦使用location.replace和location.href兩個方法,而不推薦使用location.reload和history.go(0),因為這兩個重新整理相當於按F5重新整理,有表單提交的頁面很容易會給出是否重複提交表單的提示。
3、使用Array.join在Internet Explorer中進行字串串連操作是最快的;而+= 或者 String.prototype.concat.apply(object, arguments) 在其他瀏覽器中表現得更好。
4、判斷中英文字元長度 if (string.match(/[^\x00-\xff]/ig) != null) //全形
5、計算某年某月共有多少天函數:
function days_in_month(y, m) {
return 32 - new Date(y, m - 1, 32).getDate();
}
6、jquery使所有ajax請求不緩衝方法:$.ajaxSetup({cache: false});
7、取數組最大和最小值
Math.max.apply(Math, [1,2,3]) //3
Math.min.apply(Math, [1,2,3]) //1
8、 行內元素設定padding,只有padding-left和padding-right生效,padding-top和padding-bottom無效。
比如行內元素<a>應用了padding:50px,隻影響了左右,但沒有影響上下。
9、js操作iframe
獲得iframe的window對象 :
chrome:iframeElement. contentWindow firefox: iframeElement.contentWindow
ie6:iframeElement.contentWindow
獲得iframe的document對象 :
chrome:iframeElement.contentDocumentfirefox:iframeElement.contentDocument
ie:element.contentWindow.document
10、ie6 出現橫向捲軸解決方案
html{_overflow-x:hidden;}
11、定位左右不動的捲軸
<style type="text/css">
#bannerLeft { width: 100px; height: 600px; float: left; position: relative; left: -100px; background: #ccc;}
#bannerRight {width: 100px; height: 600px; float: right; position: relative; right: -100px; background: #ccc;}
</style>
<div class="wrap" style="position:fixed;_position:absolute; top:0; left:50%; margin-left:-495px; ">
<div id="bannerLeft">banner left</div>
<div id="bannerRight">banner right</div>
</div>
12、在用到mouseover和mouseout事件來作為事件觸發的條件,但是如果我們用做觸發的元素內部有其他的元素的時候當滑鼠移上的時候會反覆 的觸發mouseover和mouseout事件。因為內部元素在滑鼠移上的時候會向它的父物件派發事件,所以外面元素相當於也觸發了mouseover 事件。
為了阻止mouseover和mouseout的反覆觸發,這裡要用到event對象的一個屬性relatedTarget,這個屬性就是用來判斷 mouseover和mouseout事件目標節點的相關節點的屬性。簡單的來說就是當觸發mouseover事件時,relatedTarget屬性代 表的就是滑鼠剛剛離開的那個節點,當觸發mouseout事件時它代表的是滑鼠移向的那個對象。由於MSIE不支援這個屬性,不過它有代替的屬性,分別是 fromElement和toElement。
return e.relatedTarget || (e.fromElement && e.fromElement === e.srcElement ? e.toElement: e.fromElement);
13、JavaScript音頻/視頻播放器
html5media(官網:http://html5media.info/)是一個JavaScript音頻/視頻播放器能夠讓每一個瀏覽器都能夠播放在HTML5多媒體標籤中定義的多媒體檔案。 對於不相容的瀏覽器,該播放將採用支援Flash的FlowPlayer播放器來代替。
14、解決iframe裡半透明的圖片,在IE下不顯示半透明問題。
在IE下,iframe裡有半透明圖片,卻不能顯示半透明效果。
這個時候可以在html中給iframe添加一個屬性:allowtransparency="true"。
如:<iframe scrolling="no" allowtransparency="true" src="test.htm"></iframe>