收集的35個 jQuery 小技巧/代碼片段,可以幫你快速開發.
17.阻止鏈接加載
有時你不希望鏈接到某個頁面或者重新加載它,你可能希望它來做一些其他事情或者觸發一些其他腳本,你可以這么做:
$('a.no-link').click(function (e) {
e.preventDefault();
});
18.切換 fade/slide
fade 和 slide 是我們在 jQuery 中經常使用的動畫效果,它們可以使元素顯示效果更好。但是如果你希望元素顯示時使用種效果,而消失時使用第二種效果,則可以這么做:
// Fade
$('.btn').click(function () {
$('.element').fadeToggle('slow');
});
// Toggle
$('.btn').click(function () {
$('.element').slideToggle('slow');
});
19.簡單的手風琴效果
這是一個實現手風琴效果快速簡單的方法:
// Close all panels
$('#accordion').find('.content').hide();
// Accordion
$('#accordion').find('.accordion-header').click(function () {
var next = $(this).next();
next.slideToggle('fast');
$('.content').not(next).slideUp('fast'); return false;
});
20.讓兩個 DIV 高度相同
有時你需要讓兩個 div 高度相同,而不管它們里面的內容多少??梢允褂孟旅娴拇a片段:
var $columns = $('.column');var height = 0;
$columns.each(function () {
if ($(this).height() > height) {
height = $(this).height();
}
});
$columns.height(height);
這段代碼會循環一組元素,并設置它們的高度為元素中的大高。
21. 驗證元素是否為空
This will allow you to check if an element is empty.
$(document).ready(function() {
if ($('#id').html()) {
// do something
}
});
22. 替換元素
Want to replace a div, or something else?
$(document).ready(function() {
$('#id').replaceWith('
<DIV>I have been replaced</DIV>
');
});
23. jQuery延時加載功能
Want to delay something?
$(document).ready(function() {
window.setTimeout(function() {
// do something
}, 1000);
});
24. 移除單詞功能
Want to remove a certain word(s)?
$(document).ready(function() {
var el = $('#id');
el.html(el.html().replace(/word/ig, ""));
});
25. 驗證元素是否存在于jquery對象集合中
Simply test with the .length property if the element exists.
$(document).ready(function() {
if ($('#id').length) {
// do something
}
});
26. 使整個DIV可點擊
Want to make the complete div clickable?
$(document).ready(function() {
$("div").click(function(){
//get the url from href attribute and launch the url
window.location=$(this).find("a").attr("href"); return false;
});// how to use<DIV><A href="index.html">home</A></DIV>});
27. ID與Class之間轉換
當改變Window大小時,在ID與Class之間切換
$(document).ready(function() {
function checkWindowSize() {
if ( $(window).width() > 1200 ) {
$('body').addClass('large');
} else {
$('body').removeClass('large');
}
}
$(window).resize(checkWindowSize);
});
28. 克隆對象
Clone a div or an other element.
$(document).ready(function() {
var cloned = $('#id').clone();// how to use<DIV id=id></DIV>});
29. 使元素居屏幕中間位置
Center an element in the center of your screen.
$(document).ready(function() {
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;
}
$("#id").center();
});
30. 寫自己的選擇器
Write your own selectors.
$(document).ready(function() {
$.extend($.expr[':'], {
moreThen1000px: function(a) {
return $(a).width() > 1000;
}
});
$('.box:moreThen1000px').click(function() {
// creating a simple js alert box
alert('The element that you have clicked is over 1000 pixels wide');
});
});
31. 統計元素個數
Count an element.
$(document).ready(function() {
$("p").size();
});
32. 使用自己的 Bullets
Want to use your own bullets instead of using the standard or images bullets?
$(document).ready(function() {
$("ul").addClass("Replaced");
$("ul > li").prepend("? "); // how to use
ul.Replaced { list-style : none; }
});
33. 引用Google主機上的Jquery類庫
Let Google host the jQuery script for you. This can be done in 2 ways.
//Example 1
<SCRIPT src="http://www.86807888.com.cn/uploads/szweb/http://www.google.com/jsapi"></SCRIPT>
<SCRIPT type=text/javascript>
google.load("jquery", "1.2.6");
google.setOnLoadCallback(function() {
// do something
});
</SCRIPT><SCRIPT type=text/javascript src="http://www.86807888.com.cn/uploads/szweb/http://ajax.googleapis.com/ajax/libs/jquery/1.2.6/jquery.min.js"></SCRIPT>
// Example 2:(the best and fastest way)
<SCRIPT type=text/javascript src="http://www.86807888.com.cn/uploads/szweb/http://ajax.googleapis.com/ajax/libs/jquery/1.2.6/jquery.min.js"></SCRIPT>
34. 禁用Jquery(動畫)效果
Disable all jQuery effects
$(document).ready(function() {
jQuery.fx.off = true;
});
35. 與其他Javascript類庫沖突解決方案
To avoid conflict other libraries on your website, you can use this jQuery Method, and assign a different variable name instead of the dollar sign.
$(document).ready(function() {
var $jq = jQuery.noConflict();
$jq('#id').show();
});
程序猿都要了解的35個 jQuery 小技巧(上)
網頁設計
企業網站建設一條龍
找零度飛易網絡公司-fslingdu所做php
網站建設方案、
網站設計、
網站制作由
北京上海深圳龍崗衢州蘭州常州東營南通濟寧桂林淮安煙臺長春無錫天津昆山蘇州合肥貴洛陽昆明天津唐山泉州惠州萬州新鄉商丘臺州哈爾濱太原攝影???/strong>隨州學校商丘廣東湖南廣西江西海南廣州企業中小企業武漢南山羅湖福田虎門肇慶汕尾汕頭廣州佛山成都杭州濟南重慶福州西安廈門昆山沈陽青島徐州鄭州南京寧南寧長沙大連淄博石家莊南昌溫州珠海番禺順德南三水高明中山東莞合肥江門嘉興西寧大良容桂倫教勒流陳村均安杏壇龍江樂從北滘祖廟石灣南莊等地區企業網站建設(廣告)公司提供專業做網站價格規劃書及營銷型網站制作,網站建設基礎知識