常用js代码

 1.  栏目隐藏和显示切换
< script language = " javascript " >
function swichFrame()
{

    
if(parent.frame1.cols=="0,*"){
    parent.frame1.cols
="170,*";
    ....

    }

    
else if(parent.frame1.cols=="170,*"){
    parent.frame1.cols
="0,*";
    .....

    }

}

</ script >

2. 选择文本框字符

function  select(istart, iend)
{
    
var oTextBox=document.getElementById("txtState");
    
//use text ranges for Internet Explorer
    if (oTextBox.createTextRange) {       
        
var oRange = oTextBox.createTextRange(); 
        oRange.moveStart(
"character", iStart); 
        oRange.moveEnd(
"character", iEnd - oTextBox.value.length);      
        oRange.select();                  
                                          
    
//use setSelectionRange() for Mozilla 
    }
 else if (oTextBox.setSelectionRange) {
        oTextBox.setSelectionRange(iStart, iEnd);
    }
                                     
                                          
    
//set focus back to the textbox       
    oTextBox.focus();
}

范例运行结果:
javascript::select(1,2)                 123456
javascript::select(6,0)                 |123456
javascript::select(6,6)                 123456|

3. 流动广告

NewsTicker.prototype.tick  =   function  ()  {
    
var iTickerLength = this.ticker.offsetWidth;
    
var oThis = this;

    
var doSetTimeout = function() {
        oThis.tick();
    }
;

    
if (this.ticker.innerHTML) {
        
if (this.ticker.offsetLeft > -iTickerLength) {
            
var iNewLeft = this.ticker.offsetLeft - 1;
            
this.ticker.style.left = iNewLeft + "px";
        }
 else {
            
this.ticker.style.left = this.tickerContainer.offsetWidth + "px";
        }

    }

    
this.timer = setTimeout(doSetTimeout,1);
}
;

4.返回上一页
<a href="javascript:history.go(-1)">

5. 激发事件

1 . function clickme()  {
        document.forms[
0].submit();
    }


2 . var btn1  =  document.forms[ 0 ].helpButton;
    btn1.fireEvent(
" onclick " );

6.事件介绍

HTML事件
{
load 
event 
{The onload event handler is called when the HTML document finishes
loading into the browser window. This 
event is commonly relied on to do any initialization
required 
in the document. For instance, if form fields need to be preloaded with text, and
that was not covered by the HTML code itself, it 
is best to wait until the document is fully
loaded before initializing those fields.
}

unload 
event
{ The unload event fires when a browser is leaving the current document.
This happens when the browser window 
is being closed or the user has moved on to
another document. The main purpose of 
this event is to perform cleanup tasks before
the document closes.
The unload 
event is commonly used in some of the seedier web locations to provide final
pop
-up advertising before a user leaves a web site. This is generally considered bad
etiquette. The unload 
event should be avoided unless absolutely necessary.
}

submit 
event
{ The submit event occurs just before a form is submitted to a web server.
It 
is common to capture this event to perform edit checking before allowing a form
submission to 
continue. Since this event can be canceled, JavaScript can stop the
form from being processed 
if everything is not in order.
}

reset 
event 
{The reset event occurs when the reset button on a form is clicked. The reset
button clears a form by resetting all the values back to their defaults.
}

select 
event
{ The select event occurs when the user selects text inside a text box or text
area. Text can be selected by holding the left mouse button down 
while dragging across
the text or by holding the SHIFT key down 
while using the arrow keys to move the cursor
across the text.
}

change 
event
{ The change event occurs when a control loses focus (the user tabs out of
it) and its value has been altered. This way, 
in text boxes the change event does not fire
for each and every keystroke when a user is entering a value but only when the user
leaves the field. This same 
event fires on radio buttons, check boxes, and select lists in
the same manner.
}

abort 
event 
{The abort event occurs when the browser stops trying to load an image
on the web page. This can occur when the user hits the browser¡¯s Stop button or clicks
a link to go to another page. In my experience, 
this event is rarely used.
}

error 
event
{ The error event occurs when an error happens while the web page is being
loaded. This could be an error specific to a particular 
object (such as a Java applet¡¯s failing
to load) or a run
-time error caused by poorly written JavaScript code. This is another event
that 
is rarely captured in most web pages.
}

resize 
event
{The resize event fires when an object is being resized¡ªfor instance, if a
browser window has been resized by the user, or a frame. There may be times when you
would like to be able to resize the individual controls inside the window based on the
available space, although capturing 
this event is still rare.
}

scroll 
event 
{For web objects that scroll, such as the browser window or a text area form
control, the scroll 
event is fired anytime the object¡¯s scroll bar is changed. This can be done
using the mouse to move the scroll bar manually, by using arrow keys, or by other means.
}

}

键盘事件
keypress           onkeypress
keydown            onkeydown
keyup                 onkeyup
鼠标事件
mousedown     onmousedown
mouseup          onmouseup
mouseover       onmouseover
mousemove     onmousemove
mouseout         onmouseout
click onclick
dblclick ondblclick
用户事件
focus onfocus               HTML 4.01
blur onblur                     HTML 4.01
activate onactivate        DOM Level 2

Other event handlers allow programmers access to events that are not specified by the standards:
1 onbeforeprint and onafterprint allow access to browser print events.
2 ondrag, ondragstart, and ondragend allow access to drag-and-drop events.
3 onmousewheel allows access to the mouse wheel movements.
4 oncut, oncopy, and onpaste allow access to copy-and-paste events.

< body  oncontextmenu ="return false"  ondragstart ="return false"  oncopy ="document.selection.empty()"  onbeforecopy ="return false"  onselectstart ="return false" >

 

6 查找控件

function  MM_findObj(n, d)  //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d
=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}

  
if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  
for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  
if(!&& d.getElementById) x=d.getElementById(n); return x;
}

7. 获取光标反选文字
var sText = document.selection.createRange();
禁止反选文字:<P unselectable="on"> </P>

< script >
function AddLink()
{//identify selected text
var sText = document.selection.createRange();
if (sText.text!=""){
  
//create link
  document.execCommand("CreateLink");
  
//replace text with URL
  if (sText.parentElement().tagName == "A"){
    sText.parentElement().innerText
=sText.parentElement().href;
    document.execCommand(
"ForeColor","false","#FF0033");
  }

}

else{
    alert(
"Please select some blue text!");
}
   
}

</ script >

< unselectable ="on" > Select any portion of the following blue text, such as "My favorite Web site". Click the button to turn the selected text into a link.  The text will be changed to the URL that you specify. </ P >< style ="color=#3366CC" > My favorite Web site is worth clicking on. Don't forget to check out my favorite music group! </ P >
< BUTTON  onclick ="AddLink()"  unselectable ="on" > Click to add link </ BUTTON >
< class ="viewsource"  unselectable ="on" > To view the source code, right-click this page and choose View Source from the shortcut menu. </ P >
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值