일상 문제 공유

7567 단어

동적 가져오기div 높이

window.onresize(){...}

$(window).resize(function(){...}


가로 화면 금지

$(function(){
//         :
function hengshuping(){
    // if(window.orientation==180||window.orientation==0){
    //     alert("    !")
    // }
    if(window.orientation==90||window.orientation==-90){
        alert("        ,       ")
    }
}
window.addEventListener("onorientationchange" in window ? "orientationchange" : "resize", hengshuping, false);
})

질문


calc () = calc (사칙 연산)
width: calc(100% - 10px)
width:-moz-calc(100% - 10px); 
width:-webkit-calc(100% - 10px);
--    
    firefox 4.0+      calc()  ,    -moz-calc()    ;
    chrome 19 dev-webkit-calc()  ;
    IE9calc();
    Opera      ~~
-->

브라우저의 시각적 높이 가져오기

   -1.  IE9+、chrome、firefox、Opera、Safari
   window.innerHeight
   
   window.innerWidth
   
   var w = document.documentElement.clientWidth || document.body.clientWidth;
   var h = document.documentElement.clientHeight || document.body.clientHeight;
   


클릭하여 관통


방법1: 이벤트.stopPropagation()


이 메서드는 이벤트가 다른 Document 노드로 디스패치되지 않도록 전파를 중지합니다.사건이 전파되는 어느 단계에서든 그것을 호출할 수 있다.이 방법은 같은 문서 노드의 다른 이벤트 핸들이 호출되는 것을 막을 수는 없지만, 이벤트를 다른 노드로 나누는 것을 막을 수 있습니다.
    
"mask">
"img">
"btn-close">
var mask = document.getElementsByClassName('mask')[0]; var img = document.getElementsByClassName('img')[0]; var btn = document.getElementsByClassName('btn-close')[0]; console.log(mask); mask.onclick = function(){ console.log(1111) event.stopPropagation() }; img.onclick = function(){ console.log(222) event.stopPropagation() }; btn.onclick = function(){ console.log(3333) event.stopPropagation() };

현재 환경이 OS인지 안드로이드인지 판단

if (/(iPhone|iPad|iPod|iOS)/i.test(navigator.userAgent)) {
    
 } else if (/(Android)/i.test(navigator.userAgent)) {
     
 } else {
    this.isIos = true
}

메모리 누설


몇 가지 흔히 볼 수 있는 메모리 유출


1. 글로벌 변수
   
   a = "aaa"
   
   function foo(){
     this.a = 'aaa'
   }
   //      ,this      
   foo();

2. 타이머나 리셋 함수를 제때에 정리하지 않음

function b() {
    var a = setInterval(function() {
        console.log("Hello");
        clearInterval(a);
        b();                
    }, 50);
}

function init()
    {
        window.ref = window.setInterval(function() { draw(); }, 50);
    }
    function draw(){
        console.log('Hello');
        clearInterval(window.ref);
        init();
    }
init();

function time(f, time) {
    return function walk() {
     clearTimeout(aeta);
        var aeta =setTimeout(function () {
            f();
            walk(); 
        }, time);
    };
}
time(updateFormat, 1000)();

3.dom에서 벗어난 인용(모든 인용 삭제)
var elements = {
    button: document.getElementById('button'),
}

function doStuff() {
    button.click();
}

function removeButton() {
    //     body      
    document.body.removeChild(document.getElementById('button'));
    //   ,          #button    
    // elements   。button         ,    GC   。
}

가방을 닫다
내용이 너무 많아서 자세한 내용은 이것을 보셔도 됩니다.csdn.net/haogexiaole…
js의 사용 팁 www.haorooms.com/post/js_shi…
전재 대상:https://juejin.im/post/5b8de30c518825430e572d1a

좋은 웹페이지 즐겨찾기