Javascript1

HTML控件元素的name屬性有什麼約束?
1.不能有空格
2.除了下劃線外不能有標點符號
3.name必須在括號之內
4.不能以數字開頭


document.getElementById("mytext").select();函數顯示的效果是用鼠標把一行字用左鍵拖動選中 一樣

getElementByTagName()函數IE不支持

爲什麼window.status設置了但是不顯示?
打開IE7,在“工具—>Internet 選項—>安全—>選中Internet”,點擊“自定義級別”,在【腳本】-【允許狀

態欄通過腳本更新】中選擇“啓用”就行了 
  window .status ="我愛你";
     return true ;


var str="Hello World!"
document.write(str.toUpperCase())


Math.max(value1,value2);取最大值
Math.pow(value,10);一個數的十次方
Math.round(value1);四捨五入的值
Math.floor(Math.random()*6)+1; 隨機取6以內的數字


如何動態產生js函數?
   var X2 = {}    //my namespace:)
    X2.Eval = function(code) {
        if (!!(window.attachEvent && !window.opera)) {
            //ie
            execScript(code);
        } else {
            //not ie
            window.eval(code);
        }
    }

    var s = 'global';
    function demo3() {
        X2.Eval(" function test() {alert('1');}");
    }
    demo3();
     test();

 

<noscript>,在不支持JavaScript 時,瀏覽器顯示<noscript>和</noscript>中的內容


如何全屏瀏覽器?
  function fullScreen() {
        this.moveTo(0, 0);
        window.resizeTo(screen.availWidth,screen.availHeight);
    }
js的screen 對象?
screen 屏幕對象 反映了當前用戶的屏幕設置。

width 返回屏幕的寬度(像素數)。

height 返回屏幕的高度。

availWidth 返回屏幕的可用寬度(除去了一些不自動隱藏的類似任務欄的東西所佔用的寬度)。

availHeight 返回屏幕的可用高度。

colorDepth 返回當前顏色設置所用的位數 - 1:黑白;8:256色;16:增強色;24/32:真彩色

Navigator瀏覽器的屏幕對象:


英文的Navigator瀏覽器的屏幕對象?
availHeight:minus permanent or semipermanent user interface features displayed by the operating

system:such as the Taskbar on Windows. 

availWidth:Specifies the width of the screen, in pixels, minus permanent or semipermanent user

interface:features displayed by the operating system, such as the Taskbar on Windows. 

colorDepth:The bit depth of the color palette, if one is in use; otherwise, the value is derived

from screen.pixelDepth. 

height:Display screen height. 

pixelDepth:Display screen color resolution (bits per pixel). 

width:Display screen width.

 


在Firefox下無onpropertychange事件,必須用input事件來替代,方法如下:

<html>
<head>
</head>
<body>
<form>
<div id="msg"></div>
<input id='txt' value="" />
<script>
//當狀態改變的時候執行的函數
function handle()
{document.getElementById('msg').innerHTML='輸入的文字長度爲:'+document.getElementById

('txt').value.length;
}
//firefox下檢測狀態改變只能用oninput,且需要用addEventListener來註冊事件。
if(/msie/i.test(navigator.userAgent))    //ie瀏覽器
{
第一種方法:document.getElementById('txt').onpropertychange=handle ;
第二種方法document.getElementById('txt').attachEvent('onpropertychange',function(o){
       alert(o.propertyName);----》這個直接獲取到o的哪一項改變了
    });
再繼續深挖下:
for(var item in o){
            alert(item+":"+o[item]);
        }----------------》你就會發現裏面有很多屬性

}
else
{//非ie瀏覽器,比如Firefox
document.getElementById('txt').addEventListener("input",handle,false);
}
</script>
</form>
</body>
</html>

判斷是否是IE7?
<script>
(is_ie && /msie 7/.0/i.test(navigator.userAgent));
</script> 

判斷是否是IE?
1.<script>
is_opera = /opera/i.test(navigator.userAgent); 
var is_ie = (/msie/i.test(navigator.userAgent) && !is_opera)
alert(is_ie);
</script> 
2.if (document.uniqueID){
ie能識別出名爲uniqueID的document對象的專用屬性,名爲uniqueID,ie是唯一能夠識別這個屬性的瀏覽器

判斷瀏覽器類型?
function getOs(){//判斷瀏覽器類型
var OsObject = "";
if(navigator.userAgent.indexOf("MSIE")>0) {
return "MSIE";
}
if(isFirefox=navigator.userAgent.indexOf("Firefox")>0){
return "Firefox";
}
if(isSafari=navigator.userAgent.indexOf("Safari")>0) {
return "Safari";
}
if(isCamino=navigator.userAgent.indexOf("Camino")>0){
return "Camino";
}
if(isMozilla=navigator.userAgent.indexOf("Gecko/")>0){
return "Gecko";


什麼是addEventListener?
element.addEventListener(type,listener,useCapture)--》element是要綁定函數的對象
type是事件名稱,要注意的是"onclick"要改爲"click","onblur"要改爲"blur",也就是說事件名不要帶"on"。
 
listener是綁定的函數了,記住不要跟括號

最後一個參數是個布爾值,表示該事件的響應順序
userCapture若爲true,則瀏覽器採用Capture,若爲false則採用bubbing方式。建議用false

<div id="div_test"> <input type="button" id="btn_test" value="se4.cn技術基地" /> </div>

window.οnlοad=function(){
 document.getElementById("div_test").addEventListener("click",test1,false);
 document.getElementById("btn_test").addEventListener("click",test2,false);
 }
function test1(){
alert("外層div觸發")
}
function test2(){
 alert("內層input觸發")
 }

如果userCapture是true則test1先觸發,如果userCapture是false則test2先觸發。

 

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章