中文字幕在线观看,亚洲а∨天堂久久精品9966,亚洲成a人片在线观看你懂的,亚洲av成人片无码网站,亚洲国产精品无码久久久五月天

7個有用的jQuery代碼片段分享

2018-07-20    來源:open-open

容器云強(qiáng)勢上線!快速搭建集群,上萬Linux鏡像隨意使用

7個有用的jQuery代碼片段分享

jQuery是一款輕量級的JavaScript庫,是最流行的客戶端HTML腳本之一,它在WEB設(shè)計師和開發(fā)者中非常的有名,并且有非常多有用的插件和技術(shù)幫助WEB開發(fā)人員開發(fā)出有創(chuàng)意和漂亮的WEB頁面。

今天我們?yōu)閖Query用戶分享一些小技巧,這些技巧將幫助你提示你網(wǎng)站布局和應(yīng)用的創(chuàng)意性和功能性。

一、在新窗口打開鏈接

用下面的代碼,你點擊鏈接即可在新窗口打開:

$(document).ready(function() {   //select all anchor tags that have http in the href   //and apply the target=_blank   $("a[href^='http']").attr('target','_blank'); });



二、設(shè)置等高的列

應(yīng)用下面的代碼,可以使得你的WEB應(yīng)用每一列高度都想等, 

<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預(yù)加載圖像

這個小技巧可以提升頁面加載圖片的速度:

//author http://www.lai18.com
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…');
});



四、禁用鼠標(biāo)右鍵

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

五、設(shè)定計時器

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



六、計算子元素的個數(shù)

<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>



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

<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>

標(biāo)簽: 代碼 腳本 開發(fā)者

版權(quán)申明:本站文章部分自網(wǎng)絡(luò),如有侵權(quán),請聯(lián)系:west999com@outlook.com
特別注意:本站所有轉(zhuǎn)載文章言論不代表本站觀點!
本站所提供的圖片等素材,版權(quán)歸原作者所有,如需使用,請與原作者聯(lián)系。

上一篇:Java上傳圖片,對圖片進(jìn)行等比例縮放,及局部裁剪的工具類代碼

下一篇:基于注解的spring MVC程序