国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 語言 > JavaScript > 正文

解析javascript 實用函數的使用詳解

2024-05-06 15:47:56
字體:
來源:轉載
供稿:網友
本篇文章對javascript中的一些實用函數進行了詳細的分析介紹,需要的朋友參考下

復制代碼 代碼如下:


function addBookMark(url, title){
//將網頁添加到收藏addBookMark(window.location,document.title)
if(document.all){
window.external.addFavorite(url,title);
}else if (window.sidebar){
window.sidebar.addPanel(title,url,'');
}else{
alert('添加失敗');
}
}


function setHomepage(obj,url) {
// setHome(this, window.location)
try {
obj.style.behavior = 'url(#default#homepage)';
obj.setHomePage(url);
} catch (e) {
if (window.netscape) {
try {
netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
} catch (e) {
alert('此操作被瀏覽器拒絕!/n請在瀏覽器地址欄輸入"about:config"并回車/n然后將 [signed.applets.codebase_principal_support]的值設置為"true",雙擊即可。');
}
var prefs = Components.classes['@mozilla.org/preferences-service;1'].getService(Components.interfaces.nsIPrefBranch);
prefs.setCharPref('browser.startup.homepage', vrl);
}
}
}

function boxMove(box){
//可移動的層
var w = box.scrollWidth,h = box.scrollHeight;
var iWidth = document.documentElement.clientWidth;
var iHeight = document.documentElement.clientHeight;
var moveX = 0,moveY = 0,moveTop = 0,moveLeft = 0,moveable = false;
box.onmousedown = function(e){
moveable = true;
e = window.event?window.event:e;
moveX = e.clientX-box.offsetLeft;
moveY = e.clientY-box.offsetTop;
box.style.zIndex++;
}
document.onmousemove = function(e){
if(moveable){
e = window.event?window.event:e;
var x = e.clientX - moveX;
var y = e.clientY - moveY;
if ( x > 0 &&( x + w < iWidth) && y > 0 && (y + h < iHeight)){
box.style.left = x + "px";
box.style.top = y + "px";
box.style.margin = "auto";
}
}
}
document.onmouseup = function (){moveable = false;};
}

function getHtmlRoot(){
/*獲得html文檔的根元素*/
if(document.compatMode.toLowerCase()=="css1compat"){
htmlRoot = document.documentElement;
}else{
htmlRoot = document.body;
}
return htmlRoot;
}


function showWindow(boxId, closeId, showBg){//彈窗
htmlRoot = getHtmlRoot();
box = document.getElementById(boxId);
boxId = '#' + boxId;
closeId = '#' + closeId;
showBox = $(boxId);
showBox.show();
z_index = 20;
moveLeft = (htmlRoot.clientWidth- box.clientWidth)/2 + htmlRoot.scrollLeft +'px';
moveTop = (htmlRoot.clientHeight - box.clientHeight)/2 + htmlRoot.scrollTop;+'px';
showBox.css({position:'absolute', left:moveLeft,zIndex:z_index, top:moveTop});

boxMove(box);

if(showBg){
objMask = document.createElement("div");
objMask.className = "BoxMask";
htmlRoot.appendChild(objMask);
objMask.style.cssText += 'position:absolute;top:0; left:0;filter:Alpha(Opacity=50);opacity:0.5;background:#AAA;';

objMask.style.zIndex = z_index -1;
objMask.style.width = htmlRoot.clientWidth + 'px';
objMask.style.height = htmlRoot.scrollHeight + htmlRoot.scrollTop + 'px';
}

$(closeId).click(function(){
showBox.hide();
mybg.style.display = "none";
});
$('.closeBtn').click = function(){
showBox.hide();
mybg.style.display = "none";
};

}

function getFormQuery(formId){
/*生成查詢字串*/
formObj = document.getElementById(formId);
var i, queryString = "", and = "", itemValue;
for(i = 0; i<formObj.length; i++ ){
var item = formObj[i];
if ( item.name!='' ){
if(item.type == 'select-one'){
itemValue = item.options[item.selectedIndex].value;
}else if ( item.type=='checkbox' || item.type=='radio'){
if ( item.checked == false ){ continue; }
itemValue = item.value;
}else if ( item.type == 'button' || item.type == 'submit' || item.type == 'reset' || item.type == 'image'){
continue;
}else{
itemValue = item.value;
}
// itemValue = escape(itemValue);
queryString += and + item.name + '=' + itemValue;
and="&";
//queryString += and + encodeURIComponent(item.name) + '=' +encodeURIComponent( itemValue);
}
}
return queryString;
}

//定義js錯誤處理函數
onerror = errHandle;
function errHandle(msg,url,line){
var txt=""
txt = "本頁中有錯誤!/n/n"
txt += "錯誤: "+ msg +"/n"
txt += "地址: " + url + "/n"
txt += "行數: " + line + "/n/n"
alert(txt);
return false;
}


function setAutoWidth(id,width,size){
//最小或最大寬度
var obj = document.getElementById(id);
if(size=='max'){
obj.style.width = (obj.clientWidth > width) ? width + "px" : "auto";
}else{
obj.style.width = (obj.clientWidth < width) ? width + "px" : "auto";
}
}

function fontScroll(id){
/*向左滾動的文字 */
var obj = document.getElementById(id);
var text = obj.innerHTML;
var first = text.charAt(0);
var left = text.substring(1, text.length);
obj.inerHTML = left + first;
//這個寫函數之外 setInterval('fontScroll(id)', 500);
}

function bubbleSort(arr){
/*冒泡排序法*/
var sign = false // 初始化換位標記為假
for(var i=0; i < arr.length-1; i++){
for(varj=0; j < arr.length-1-i; j++){
if(arr[j]> arr[j+1]){
vartemp = arr[j]
arr[j]= arr[j+1]
arr[j+1]= temp
sign= true // 若當前這一圈比較中,有過換位,則置換位標記為真
}
}
if(sign)// 判斷當前這一圈有過換位否…
sign= false // 有過換位,則重置換位標記為假
else
break// 沒有,則終止
}
return arr;
}

function getCoordinate(evt){
/*獲得光標的坐標*/
var x = evt.clientX;
var y = evt.clientY;
document.getElementById('show').innerHTML = x +' &' + y;
}

function checkEmail(email){
/*檢測多個以分號(;)分隔的Email格式*/
if(email != null){
if(email.indexOf(";",0) == -1){ //indexOf(被查找,起始位)返某個字符串在源字符串中首次出現的位置,失敗返回-1
if(!isEmail(email)){
alert("單個郵件格式有誤,請重輸!");
document.getElementById("email").focus();
return false;
}
}else{
var emailArr = email.split(";");//split(分隔段,數組總長度)字符串分割成字符串數組
var i, iMax = emailArr.length;
for(i = 0; i < iMax; i++){
if(emailArr[i] != null || emailArr != ""){
if(!isEmail(emailArr[i])){
alert("多個郵箱格式中有郵箱格式不正確,請重新核對后再輸入!");
document.getElementById("email").focus();
return false;
}
}
}
}
}
function isEmail(str){
var reg = /^(/w)+(/./w+)*@(/w)+((/./w+)+)$/;
return reg.test(str);//test(被測字串)檢測一個字符串是否匹配某個模式.
}
}

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表

圖片精選

主站蜘蛛池模板: 英超| 济宁市| 洪泽县| 白沙| 鄯善县| 南丰县| 孝义市| 崇文区| 江西省| 平阴县| 会东县| 新泰市| 长子县| 洮南市| 嘉定区| 罗平县| 襄垣县| 丁青县| 西乡县| 康乐县| 晋宁县| 武夷山市| 龙陵县| 邵东县| 天柱县| 景德镇市| 张家港市| 泰顺县| 灵台县| 瑞金市| 田阳县| 芷江| 蛟河市| 怀宁县| 望奎县| 白山市| 连平县| 阜阳市| 化州市| 宁河县| 朝阳市|