首頁  >  文章  >  web前端  >  JQ 常見DEMO

JQ 常見DEMO

高洛峰
高洛峰原創
2016-11-04 16:45:271111瀏覽

1:

//這段程式碼展示了在使用者未輸入值時,
//如何在文字類型的input域中保留
//一個預設值
wap_val = [];
$(". swap").each(function(i){
wap_val[i] = $(this).val();
$(this).focusin(function(){
if ($(this).val() = = swap_val[i]) {
$(this).val("");
}
}).focusout(function(){
if ($.trim($(this).val()) == " ") {
$(this).val(swap_val[i]);
}});});

2:

var el = $('#id');
el.html(elel .html().replace(/word/ig, ''));

3:

$('button.someClass').live('click', someFunction);
//注意,在jQuery 1.4.2中,delegate和undelegate選項
//被引入代替live,因為它們提供了更好的上下文支援
//例如,就table來說,以前你會用
//.live()
$( "table").each(function(){
$("td", this).live("hover", function(){
$(this).toggleClass("hover");
});
} );
//現在用
$("table").delegate("td", "hover", function(){
$(this).toggleClass("hover");
});

44 :

如何動態地加入已建立的元素到DOM:

var newDiv = $('');  

    newDiv.attr('id','myNewDiv').

5:

var cloned = $('#somediv').clone(); 

 

. true') {  

    //此元素是可見的  

}    this.css('position',' absolute');  

    this.css('top', ( $(window).height() - this.height() ) / +$(window).scrollTop() css('left', ( $(window).width() - this.width() ) / 2+$(window).scrollLeft() + 'px');  /這樣來使用上面的函數:  

$(element).center();  

8:

如何把有著某個特定名稱的所有元素的值都放到一個陣列中:

如何將所有具有某個特定名稱的元素的值都放到一個陣列中: = new Array();

$("input[name='table[]']").each(function(){

arrInputValues.push($(this).val());

});

9:

在jQuery中如何使用.siblings()來選擇同儕元素

$('#nav li').click(function(){

$(''#hom').reovem Class ('active');$(this).addClass('active');});

//替代做法是

$('#nav li').click(function(){

$(this) .addClass('active').siblings().removeClass('active');

});

 

10:

 ).click(function() {  

    $("input[type=checkbox]").attr("checked",!tog);  

11  :



如何取得滑鼠墊遊標位置x和y

$(document).ready(function() {
$(document).mousemove(function(e){
$('#XY').html(”X Axis : ” + e.pageX + ” | Y Axis ” + e.pageY);
});
});

 

12:

如何將整個的列表元素(List Element,LILI%)成可點擊的

$("ul li").click(function(){

window.location=$(this).find("a").attr("href");

return false;

} );

13:

如何檢查映像是否已完全載入其中

$('#theImage').attr('src', 'image.jpg').load(function() {alert) {

alert ('This Image Has Been Loaded');

});


14:


如何檢查cookie是否啟用

var dt = new Date();

dt.setSeconds(dt.Seconds() + 60) ;

document.cookie = "cookietest=1; expires=" + dt.toGMTString();var cookiesEnabled = document.cookie.indexOf("cookietest=") != -1;if(!cookiesEnabled) {

/ /沒有啟用cookie

}

 


15;

如何讓cookie過期:

var date = new Date();

date.setTime(date. ;

$.cookie('example', 'foo', { expires: date });



陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
上一篇:javascript函數下一篇:javascript函數