解析javascript 實用函數的使用詳解_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"并回车
然后将 [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
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"
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){
/*Bubble sort method*/
var sign = false // Initialize the transposition sign to 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 there is a transposition in the current circle comparison , then the substitution mark is true
}
}
if(sign)// Determine whether there has been a substitution in the current circle...
sign= false // If there has been a substitution, retry The replacement bit is marked as false
else
break// If not, terminate
}
return arr;
}
function getCoordinate(evt){
/*Get the coordinates of the cursor*/
var x = evt.clientX;
var y = evt.clientY;
document.getElementById('show' ).innerHTML = x ' &' y;
}
function checkEmail(email){
/*Detect multiple email formats separated by semicolons (;)*/
if(email != null){
if(email.indexOf(" ;",0) == -1){ //indexOf(searched, starting position) returns the position where a certain string first appears in the source string. If it fails, -1
is returned if(!isEmail(email )){
alert("The format of the single email is incorrect, please re-enter!"); > }else{
var emailArr = email.split(";");//split (separated segment, total length of array) string is divided into string array
var i, iMax = emailArr.length;
for(i = 0; i < iMax; i ){
if(emailArr[i] != null || emailArr != ""){
if(!isEmail(emailArr[i]) ){
alert("One of the multiple email formats is incorrect, please check again before entering!");
document.getElementById("email").focus();
return false ;
}
}
}
}
}
}
function isEmail(str){
var reg = /^(w) (.w )*@(w) ((.w ) )$/;
return reg.test(str);//test (tested string) detects whether a string matches a certain pattern.
}
}

熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

Video Face Swap
使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章

熱工具

記事本++7.3.1
好用且免費的程式碼編輯器

SublimeText3漢化版
中文版,非常好用

禪工作室 13.0.1
強大的PHP整合開發環境

Dreamweaver CS6
視覺化網頁開發工具

SublimeText3 Mac版
神級程式碼編輯軟體(SublimeText3)

Go語言提供了兩種動態函數創建技術:closures和反射。 closures允許存取閉包作用域內的變量,而反射可使用FuncOf函數建立新函數。這些技術在自訂HTTP路由器、實現高度可自訂的系統和建置可插拔的元件方面非常有用。

在C++函數命名中,考慮參數順序至關重要,可提高可讀性、減少錯誤並促進重構。常見的參數順序約定包括:動作-物件、物件-動作、語意意義和遵循標準函式庫。最佳順序取決於函數目的、參數類型、潛在混淆和語言慣例。

1. SUM函數,用於對一列或一組單元格中的數字進行求和,例如:=SUM(A1:J10)。 2、AVERAGE函數,用於計算一列或一組儲存格中的數字的平均值,例如:=AVERAGE(A1:A10)。 3.COUNT函數,用於計算一列或一組單元格中的數字或文字的數量,例如:=COUNT(A1:A10)4、IF函數,用於根據指定的條件進行邏輯判斷,並返回相應的結果。

C++函數中預設參數的優點包括簡化呼叫、增強可讀性、避免錯誤。缺點是限制靈活性、命名限制。可變參數的優點包括無限彈性、動態綁定。缺點包括複雜性更高、隱式型別轉換、除錯困難。

C++中的函數傳回參考類型的好處包括:效能提升:引用傳遞避免了物件複製,從而節省了記憶體和時間。直接修改:呼叫方可以直接修改傳回的參考對象,而無需重新賦值。程式碼簡潔:引用傳遞簡化了程式碼,無需額外的賦值操作。

自訂PHP函數與預定義函數的差異在於:作用域:自訂函數僅限於其定義範圍,而預定義函數可在整個腳本中存取。定義方式:自訂函數使用function關鍵字定義,而預先定義函數則由PHP核心定義。參數傳遞:自訂函數接收參數,而預先定義函數可能不需要參數。擴充性:自訂函數可以根據需要創建,而預定義函數是內建的且無法修改。

C++中的異常處理可透過自訂異常類別增強,提供特定錯誤訊息、上下文資訊以及根據錯誤類型執行自訂操作。定義繼承自std::exception的異常類,提供特定的錯誤訊息。使用throw關鍵字拋出自訂異常。在try-catch區塊中使用dynamic_cast將捕獲到的異常轉換為自訂異常類型。在實戰案例中,open_file函數會拋出FileNotFoundException異常,捕捉並處理該異常可提供更具體的錯誤訊息。
