成人精品一区二区三区中文字幕-成人精品一区二区三区-成人精品一级毛片-成人精品亚洲-日本在线视频一区二区-日本在线视频免费

導航首頁 ? 技術教程 ? 分享12個實用的jQuery代碼片段
全站頭部文字 我要出現在這里
分享12個實用的jQuery代碼片段 653 2024-03-10   

jQuery是一款優秀的JavaScript庫,它在WEB開發者和網頁設計師中非常出名,幫助網頁設計師開發出很多有創意和漂亮的WEB頁面。

本文主要分享了12個有用的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');
});
 

二、設定計時器

$(document).ready(function() {
 window.setTimeout(function() {
 // some code
 }, 500);
});

三、設置等高的列

使用下面的代碼,可以使得你的網頁應用每一列高度都一樣:

<div class="equalHeight" style="border:1px solid">
 <p>First Line</p>
 <p>Second Line</p>
 <p>Third Line</p>
</div>
<div class="equalHeight" style="border:1px solid">
 <p>Column Two</p>
</div>
<script src="http://apps.bdimg.com/libs/jquery/1.11.1/jquery.min.js"></script>
<script>
$(document).ready(function() {
 equalHeight('.equalHeight');
});
//global variable, this will store the highest height value
var maxHeight = 0;
function equalHeight(col) {
 //Get all the element with class = col
 col = $(col);
 //Loop all the col
 col.each(function() {
 //Store the highest value
 if ($(this).height() > maxHeight) {
 maxHeight = $(this).height();
 }
 });
 //Set the height
 col.height(maxHeight);
}
</script>

四、jQuery預加載圖像

這個技巧可以加快網頁加載圖片的速度:

jQuery.preloadImagesInWebPage = function() {
 for (var ctr = 0; ctr & lt; arguments.length; ctr++) {
 jQuery("").attr("src", arguments[ctr]);
 }
}
// 使用方法:
$.preloadImages("image1.gif", "image2.gif", "image3.gif");
// 檢查圖片是否被加載
$('#imageObject').attr('src', 'image1.gif').load(function() {
 alert('The image has been loaded…');
});

五、把元素定位到頁面中間

<div id="foo" style="width:200px;height: 200px;background: #ccc;"></div>
<script src="http://apps.bdimg.com/libs/jquery/1.11.1/jquery.min.js"></script>
<script type="text/javascript">
jQuery.fn.center = function() {
 this.css("position", "absolute");
 this.css("top", ($(window).height() - this.height()) / 2 + $(window).scrollTop() + "px");
 this.css("left", ($(window).width() - this.width()) / 2 + $(window).scrollLeft() + "px");
 return this;
}
//Use the above function as:
$('#foo').center();
</script>
 

六、禁用鼠標右鍵

$(document).ready(function() {
 //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;
 });
});

七、計算子元素的個數

<div id="foo">
 <div id="bar"></div>
 <div id="baz">
 <div id="biz"></div>
 <span><span></span></span>
 </div>
</div>
<script src="http://apps.bdimg.com/libs/jquery/1.11.1/jquery.min.js"></script>
<script type="text/javascript">
 //jQuery code to count child elements $("#foo > div").size()
alert($("#foo > div").size())
</script>

八、更改樣式列表

這段代碼將幫助你更改樣式列表。

 $(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')); 
 }); 
 });

九、使用jQuery設定文本大小

加入這段代碼,用戶可根據需求重新設定文本尺寸(增加或減少)。

 $(document).ready(function() {
 //find the current font size
 var originalFontSize = $('html').css('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').css('font-size', newFontSize);
 return false;
 });

//Decrease the Text Size
 $(".decreaseFont").click(function() {
 var currentFontSize = $('html').css('font-size');
 var currentFontSizeNum = parseFloat(currentFontSize, 10);

var newFontSize = currentFontSizeNum*0.8;
 $('html').css('font-size', newFontSize);
 return false;
 });

// Reset Font Size
 $(".resetFont").click(function(){
 $('html').css('font-size', originalFontSize);
 });
 });

十、檢測當前鼠標坐標

使用這個JS代碼,你可以在任何瀏覽器里獲取鼠標坐標。

 $(document).ready(function() {
 $().mousemove(function(e)
 {
 $('# MouseCoordinates ').html("X Axis Position = " + e.pageX + " and Y Axis Position = " + e.pageY);
 });

十一、獲取鼠標指針的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);
 });

十二、返回到頂部鏈接

此代碼對于比較長的頁面非常實用,用戶不必拉滾動條來返回頂部,直接點擊“返回頂部”即可。

 $(document).ready(function() {
 //when the id="top" link is clicked
 $('#top').click(function() {
 //scoll the page back to the top
 $(document).scrollTo(0,500);
 }
 });

jQuery是JavaScript最好的庫之一,支持Ajax及HTML 腳本客戶端,主要用于事件處理及制作動畫。另外,jQuery還擁有各種插件,可以讓開發者在最快的時間內創建網頁。

希望本文所述對大家學習jquery程序設計有所幫助。



主站蜘蛛池模板: 秀人网美女屋| 小淘气尼古拉| 美丽女波士1983| 红电视剧演员表| 瑜伽焰口全集 简体字| 黄色网址视频免费| 2024独一无二头像| 误杀2剧情详细介绍| 安多卫视直播在线观看| 广州打折网| 李洋演员个人简介图片| 上门女婿电影完整版免费| 金秘书为什么那样| 膨腹爱好者撑肚子视频| 张颜齐| 欠工资不给打什么电话能最快处理| 卧虎演员表| 电影双面情人| 鬼龙院花子的一生| 2025最好运头像| 男保姆| 告别信| 同志电影网站| 帕罗| 国有企业党建讲话原文| 布谷鸟 电影| 年会不能停免费观看完整版电影| 唐人街探案网剧1| 韩佳熙的电影全部作品| 所求皆所愿| 食人鱼电影| 演员于晓光| 中国的省份有哪些?| 夫人电影| 饥渴女人的外遇| 孤独感拉满的头像| 《欢·爱》郭晓东| 《最美的青春》演员表| xmx| 麻辣隔壁第一季| 黄视频免费看网站|

!!!站長長期在線接!!!

網站、小程序:定制開發/二次開發/仿制開發等

各種疑難雜癥解決/定制接口/定制采集等

站長微信:lxwl520520

站長QQ:1737366103