收集的35個(gè) jQuery 小技巧/代碼片段,可以幫你快速開發(fā).
17.阻止鏈接加載
有時(shí)你不希望鏈接到某個(gè)頁面或者重新加載它,你可能希望它來做一些其他事情或者觸發(fā)一些其他腳本,你可以這么做:
$('a.no-link').click(function (e) {
e.preventDefault();
});
18.切換 fade/slide
fade 和 slide 是我們?cè)?jQuery 中經(jīng)常使用的動(dòng)畫效果,它們可以使元素顯示效果更好。但是如果你希望元素顯示時(shí)使用種效果,而消失時(shí)使用第二種效果,則可以這么做:
// Fade
$('.btn').click(function () {
$('.element').fadeToggle('slow');
});
// Toggle
$('.btn').click(function () {
$('.element').slideToggle('slow');
});
19.簡單的手風(fēng)琴效果
這是一個(gè)實(shí)現(xiàn)手風(fēng)琴效果快速簡單的方法:
// 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.讓兩個(gè) DIV 高度相同
有時(shí)你需要讓兩個(gè) div 高度相同,而不管它們里面的內(nèi)容多少??梢允褂孟旅娴拇a片段:
var $columns = $('.column');var height = 0;
$columns.each(function () {
if ($(this).height() > height) {
height = $(this).height();
}
});
$columns.height(height);
這段代碼會(huì)循環(huán)一組元素,并設(shè)置它們的高度為元素中的大高。
21. 驗(yàn)證元素是否為空
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延時(shí)加載功能
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. 驗(yàn)證元素是否存在于jquery對(duì)象集合中
Simply test with the .length property if the element exists.
$(document).ready(function() {
if ($('#id').length) {
// do something
}
});
26. 使整個(gè)DIV可點(diǎn)擊
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之間轉(zhuǎn)換
當(dāng)改變Window大小時(shí),在ID與Class之間切換
$(document).ready(function() {
function checkWindowSize() {
if ( $(window).width() > 1200 ) {
$('body').addClass('large');
} else {
$('body').removeClass('large');
}
}
$(window).resize(checkWindowSize);
});
28. 克隆對(duì)象
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. 統(tǒng)計(jì)元素個(gè)數(shù)
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主機(jī)上的Jquery類庫
Let Google host the jQuery script for you. This can be done in 2 ways.
//Example 1
<SCRIPT src="http://www.qy-mjzn.com/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.qy-mjzn.com/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.qy-mjzn.com/uploads/szweb/http://ajax.googleapis.com/ajax/libs/jquery/1.2.6/jquery.min.js"></SCRIPT>
34. 禁用Jquery(動(dòng)畫)效果
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個(gè) jQuery 小技巧(上)
網(wǎng)頁設(shè)計(jì)
企業(yè)網(wǎng)站建設(shè)一條龍
找零度飛易網(wǎng)絡(luò)公司-fslingdu所做php
網(wǎng)站建設(shè)方案、
網(wǎng)站設(shè)計(jì)、
網(wǎng)站制作由
北京上海深圳龍崗衢州蘭州常州東營南通濟(jì)寧桂林淮安煙臺(tái)長春無錫天津昆山蘇州合肥貴洛陽昆明天津唐山泉州惠州萬州新鄉(xiāng)商丘臺(tái)州哈爾濱太原攝影???/strong>隨州學(xué)校商丘廣東湖南廣西江西海南廣州企業(yè)中小企業(yè)武漢南山羅湖福田虎門肇慶汕尾汕頭廣州佛山成都杭州濟(jì)南重慶福州西安廈門昆山沈陽青島徐州鄭州南京寧南寧長沙大連淄博石家莊南昌溫州珠海番禺順德南三水高明中山東莞合肥江門嘉興西寧大良容桂倫教勒流陳村均安杏壇龍江樂從北滘祖廟石灣南莊等地區(qū)企業(yè)網(wǎng)站建設(shè)(廣告)公司提供專業(yè)做網(wǎng)站價(jià)格規(guī)劃書及營銷型網(wǎng)站制作,網(wǎng)站建設(shè)基礎(chǔ)知識(shí)