Javascript部分常用技巧

來源:互聯網
上載者:User

定時自動轉頁
<script>setTimeout('document.location.href="[url]"',5000)</script>

返回前一頁
<a href="javascript:history.back()">返 回</a>

關閉視窗
<script>javascript:window.close()</script>

彈出訊息視窗
<script>window.alert("hi!");</script>")

定焦點到文字框的末尾
function focusEnd(){
  if(field.createTextRange){
    var  r  = field.reateTextRange();
    r.moveStart('character',field.value.length);
    r.collapse();
    r.select();
  }

規範文章中的圖片
<img onload='javascript:if(this.width>790)this.style.width=790;'
onclick='javascript:window.open(this.src);'
style='CURSOR:hand'
onmousewheel='return bbimg(this)'>
function bbimg(o){
var zoom=parseInt(o.style.zoom, 10)||100;zoom+=event.wheelDelta/12;if (zoom>0) o.style.zoom=zoom+'%';
return false;
}

js檔案中的中文問題
web application的編碼規則是utf-8,那麼js檔案中如果有中文就會出現亂碼(有可能是其它的頁面不正常的情況)
解決這個問題請在引用javascript的地方加上charset="gb2312"或者charset="big5"
例如:<script type="text/javascript" language="javascript" src="scripts/function.js" charset="gb2312"></script>

視窗無邊框
<a href="#" onclick="window.open('test.htm','','width=300,height=450,toolbar=0,menubar=0,location=0')">開啟</a>

輸入框長度隨輸入內容的長度而改變
<input  type="text"  style="overflow-x:visible;width:60;">

點擊單選框就直接啟用連結
第一步:把如下代碼加入到<head>地區中
<SCRIPT>
function go(loc) {
window.location.href = loc;
}
</script>
第二步:把如下代碼加入到<body>地區中
<form name="form">
    <input type="radio" name="loc" onClick="go('#');">
    選中即激發了超連結<br>
    <input type="radio" name="loc" onClick="go('#l');">
    選中即激發了超連結<br>
    <input type="radio" name="loc" onClick="go('#');">
    選中即激發了超連結<br>
</form>

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.