7個有用的jQuery程式碼片段分享

來源:互聯網
上載者:User

7個有用的jQuery程式碼片段分享

   這篇文章主要介紹了7個有用的jQuery技巧分享,本文給出了在新視窗開啟連結、設定等高的列、jQuery預先載入映像、禁用滑鼠右鍵、設定計時器等實用程式碼片段,需要的朋友可以參考下

  jQuery是一款輕量級的JavaScript庫,是最流行的用戶端HTML指令碼之一,它在WEB設計師和開發人員中非常的有名,並且有非常多有用的外掛程式和技術協助WEB開發人員開發出有創意和漂亮的WEB頁面。

  今天我們為jQuery使用者分享一些小技巧,這些技巧將協助你提示你網站布局和應用的創意性和功能性。

  一、在新視窗開啟連結

  用下面的代碼,你點選連結即可在新視窗開啟:

  ?

1

2

3

4

5

$(document).ready(function() {

//select all anchor tags that have http in the href

//and apply the target=_blank

$("a[href^='http']").attr('target','_blank');

});

  二、設定等高的列

  應用下面的代碼,可以使得你的WEB應用每一列高度都想等:

  ?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

<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預先載入映像

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

  ?

1

2

3

4

5

6

7

8

9

10

11

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…');

});

  四、禁用滑鼠右鍵

  ?

1

2

3

4

5

6

7

8

9

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

});

});

  五、設定計時器

  ?

1

2

3

4

5

$(document).ready(function() {

window.setTimeout(function() {

// some code

}, 500);

});

  六、計運算元元素的個數

  ?

1

2

3

4

5

6

7

8

9

10

11

12

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

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

  ?

1

2

3

4

5

6

7

8

9

10

11

12

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

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.