開發中可能會用到的jQuery小技巧_jquery
May 16, 2016 pm 04:56 PM1) 禁止右鍵
在開發 Web 應用的時候,有些情況需要停用右鍵功能。使用此程式碼,jQuery 開發人員可以在網頁上停用滑鼠右鍵。程式碼如下:
複製程式碼 程式碼如下:
$(document).read ) {
//catch the right-click context menu
$(document).bind("contextmenu",function(e) {
//warning prompt - optional
alert("No right -clicking!");
//delete the default context menu
return false;
});
});
2) 文字縮放
使用下面的程式碼,使用者可以更有需要增加或縮放網頁中的字體大小,程式碼如下:
複製程式碼 程式碼如下:
$(document).ready(function() {
//find the current font size
var originalFontSize = $('html').css('html').css('html').css('html').css('html').css('html').css('html').css('html'). 'font-size');
//Increase the text size
$(".increaseFont").click(function() {
var currentFontSize = $('html').css ('font-size');
var currentFontSizeNumber = parseFloat(currentFontSize, 10);
var newFontSize = currentFontSizeNumber*1.2;
$('html').$('html").s('html" ', newFontSize);
return false;
});
//Decrease the Text Size
$(".decreaseFont").click(function() {
var currentFontSize = $('html').css('font-size');
var currentFontSizeNum = parseFloat(currentFontSize, 10);
var newFontSize = currentFontFontizeNum*0.8; html').css('font-size', newFontSize);
return false;
});
// Reset Font Size
$(".resetFont").click (function(){
$('html').css('font-size', originalFontSize);
});
});
使用這個jQuery 程式碼,使用者會點擊你的網站的任何連結都會在新的視窗中開啟。如下:
複製程式碼 程式碼如下:
$(document).ready(function() {
//select all anchor tags that have http in the href
//and apply the target=_blank
$("a[href^='http']").attr('target' ,'_blank');
});
4) 樣式表切換//select all anchor tags that have http in the href
//and apply the target=_blank
$("a[href^='http']").attr('target' ,'_blank');
});
你知道網站換膚是怎麼做的嗎?下面的程式碼可以幫助你實現樣式表切換功能,如下:
複製程式碼 程式碼如下:
程式碼如下:
$(document).ready(function() {
$("a.cssSwap").click(function() {
//swap the link rel attribute with the value in the rel
$('link[rel=stylesheet]').attr('href' , $(this).attr('rel'));
});
}); $(document).ready(function() {
$("a.cssSwap").click(function() {
//swap the link rel attribute with the value in the rel
$('link[rel=stylesheet]').attr('href' , $(this).attr('rel'));
});
5) 回到頂部
程式碼如下:
$read(document). (function() {
//when the id="top" link is clicked
$('#top').click(function() {
//scoll the page back to the top
$(document).scrollTo(0,500);
}
}); $read(document). (function() {
//when the id="top" link is clicked
$('#top').click(function() {
//scoll the page back to the top
$(document).scrollTo(0,500);
}
6) 取得老鼠的X、Y 座標
程式碼如下:
$(). mousemove(function(e){
//display the x and y axis values inside the P element
$('p').html("X Axis : " e.pageX " | Y Axis " e. pageY);
}); $(). mousemove(function(e){
//display the x and y axis values inside the P element
$('p').html("X Axis : " e.pageX " | Y Axis " e. pageY);
7) 偵測目前滑鼠的座標
程式碼如下:
$(document).ready(function() {
$(document).ready(function() {
$().mousemove(function(e){ $('# MouseCoordinates ').html("X Axis Position = " e.pageX " and Y Axis Position = " e.pageY); }) ; $(document).ready(function() {
$(document).ready(function() {
8) 預先載入圖片
這個圖片預先載入片段讓你能夠快速的預先載入圖片,不需要等待。程式碼如下:
複製程式碼 程式碼如下:
jQuery.preloadI. 🎜>for(var ctr = 0; ctr
}
}
複製程式碼 程式碼如下:
程式碼如下:
("image1.gif", "image2.gif", "image3.gif");
判斷圖片是否已載入:
$('#imageObject').attr('src', 'image1.gif ').load(function() {
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn

熱門文章
倉庫:如何復興隊友
3 週前
By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.能量晶體解釋及其做什麼(黃色晶體)
1 週前
By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island冒險:如何獲得巨型種子
3 週前
By 尊渡假赌尊渡假赌尊渡假赌
擊敗分裂小說需要多長時間?
3 週前
By DDD
公眾號網頁更新緩存難題:如何避免版本更新後舊緩存影響用戶體驗?
3 週前
By 王林

熱門文章
倉庫:如何復興隊友
3 週前
By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.能量晶體解釋及其做什麼(黃色晶體)
1 週前
By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island冒險:如何獲得巨型種子
3 週前
By 尊渡假赌尊渡假赌尊渡假赌
擊敗分裂小說需要多長時間?
3 週前
By DDD
公眾號網頁更新緩存難題:如何避免版本更新後舊緩存影響用戶體驗?
3 週前
By 王林

熱門文章標籤

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

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

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

Dreamweaver CS6
視覺化網頁開發工具

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