積累的一些代碼片斷/小知識

★jQuery1.7.2下操做object元素報錯web

jQuery1.7.2下,用$選擇器操做object元素會報錯:代碼以下:ide

$('object').hide();

換用原生js就沒事了:函數

document.getElementsByTagName('object')[0].style.display = 'none';

 

★關閉按鈕小叉的字體字體

作一個簡單的關閉按鈕時,咱們常常用字母X來實現,爲了讓它看上去更像一個叉,可設置字體爲:this

font-family: 「Microsoft JhengHei」,」microsoft yahei」,Monaco,Menlo,Consolas,」Courier New」,monospace;

 

★this 的值是在函數運行時肯定的url

<button id="btn1">button1</button>
<button id="btn2">button2</button>
var obj = {
    init : function(){
        $('#btn1').click(this.alert);
    },
    init2 : function(){
        var _this = this;
        $('#btn2').click(function(){
            _this.alert();
        });
    },
    alert : function(){
        alert(this);
    }
}
         
obj.init();
obj.init2();

 

★使用正則實現數字千分位分割spa

不帶小數點的:code

"15000000".split("").reverse().join("").replace(/(\d{3})/g, "$1,").split("").reverse().join("");

帶小數點的:blog

'123123211312.333123'.replace(/(?=(?!^)(?:\d{3})+(?:\.|$))(\d{3}(\.\d+$)?)/g,',$1');

 

★背景半透明,內容不透明的寫法事件

<div><p>不透明</p></div>
div{background:rgba(0,0,0,0.2) none repeat scroll !important; /*實現FF背景透明,文字不透明*/
background:#000; filter:Alpha(opacity=20);/*實現IE背景透明*/
width:500px; height:500px; color:#F30; font-size:32px; font-weight:bold;}
div p{ position:relative;}/*實現IE文字不透明*/

火狐咱們直接用rgba顏色就能夠解決子標籤跟着半透明的問題了,可是ie還不是能很好的支持。

因此咱們給不想被透明的標籤設置一個定位屬性,問題接能解決了。

 

★爲div元素觸發keydown事件

div元素沒法觸發keydown的緣由是圖沒法被focus,處理的方式是給div加上屬性tabindex就能夠了。tabindex的取值爲整數,表示按tab鍵的時候元素得到焦點的順序。當取值爲-1時,按tab沒法得到焦點,但能夠用js代碼來focus和blur,同時,仍是能夠觸發keydown事件的。

因此要讓div能觸發keydown事件,只需以下代碼:

<div tabindex="-1">hello world !</div>

加此屬性後div會有高亮外框出現,加outline:none;可解決。

 

★查詢url中的參數

function getParameterByName(name) {
    var match = RegExp('[?&]' + name + '=([^&]*)')
                    .exec(window.location.search);
    return match && decodeURIComponent(match[1].replace(/\+/g, ' '));
}

 

★移動端,綁定了click事件的元素在點擊時會出現亮框,下面的代碼能夠去掉:

-webkit-tap-highlight-color:rgba(0,0,0,0);
-webkit-tap-highlight-color: transparent; /* For some Androids */
相關文章
相關標籤/搜索