jquery

本文主要介绍了jQuery的使用方法,包括基本操作、选择器、属性操作、事件绑定等。还讲解了js原生对象和jq对象的相互转化。同时展示了jQuery在元素追加、替换、动画效果以及Ajax请求等方面的应用,为前端开发提供了实用的参考。

//1.jq如何使用
//$("选择器").动作()
//console.log($("#myDiv"));
//console.log(document.getElementById("myDiv"));

/*
js操作
var arr = document.getElementsByTagName("p");
for(var i = 0; i<arr.length;i++){
arr[i].innerHTML = "aaa";
} */

/*
jq操作
var arr = $("#myDiv").children();
for(var i = 0; i<arr.length;i++){
arr[i].innerHTML = "aaa";
} */

//2.js原生对象和jq对象的相互转化
//jq对象-->js原生对象
var jqObj = $("#myDiv");
var jsObj = jqObj[0];
console.log(jsObj);

//js原生对象-->jq对象
var jsObj2 = document.getElementById("myDiv");
var jqObj2 = $(jsObj2);
console.log(jqObj2);

 

//写法

function testId(){
$("#myDiv").html("aaa");
}

function testClass(){
$(".myClass").html("aaa")
}

function testHouDai(){
$("form input").val("aaaaaaa");
}

function testHouDai2(){
$("form > input").val("aaaaaaa");
}

function testNextSibling(){
$("form + input").val("aaaaaaa");
}

function testNextSiblings(){
$("form ~ input").val("aaaaaaa");
}

function testChoose(){
//$("ul li:first").html("aaaaaaa");
//$("ul li:last").html("aaaaaaa");
//$("ul li:even").html("aaaaaaa");
//$("ul li:odd").html("aaaaaaa");
//$("ul li:eq(2)").html("aaaaaaa");
//$("ul li:gt(2)").html("aaaaaaa");
//$("ul li:lt(2)").html("aaaaaaa");
}

function testChoose2(){
$(":header").css("background", "#EEE");
}

function testChoose3(){
//$("input[id]").val("aaaaaaaaa");
//$("input[id=id1]").val("aaaaaaaaa");
//$("input[id!=id1]").val("aaaaaaaaa");
//$("input[name^=news]").val("aaaaaaaaa");
//$("input[name$=ter]").val("aaaaaaaaa");
//$("input[name*=ce]").val("aaaaaaaaa");
$("input[id][name]").val("aaaaaaaaa");
}

function testSplitColor(){
$("tr:odd").css("background", "#EEE");
$("tr:even").css("background", "grey");
}

function testVal(){
/* console.log($("#id1").val())
console.log(document.getElementById("id1").value) */

/* $("#id1").val("bbbaaa"); */

/* $("#id1").val(function(){
return "aaa";
}) */

}

function testText_Html(){
// text() --> innerText
// html() --> innerHTML

/* console.log($("#div1").text());
console.log($("#div1").html()); */

//$("#div1").text("<img src='1.jpg'>");
//$("#div1").html("<img src='1.jpg'>");

/*$("#div1").html(function(){
return "<img src='1.jpg'>";
}) */
}

function testAddClass(){
$("#div1").addClass("clored");
}

function testRemoveClass(){
$("#div2").removeClass("clored");
}

function testToggleClass(){
$("#div1").toggleClass("clored");
}

function testAttr_Prop(){
//attr() --> setAttribute()/getAttribute()/removeAttribute()
//prop() --> “.”
console.log($("#ipt").attr("abcd"));
console.log($("#ipt").prop("abcd"));

console.log($("#ipt").attr("value"));
console.log($("#ipt").prop("value"));
}

 

function testVal() {
//$("li").eq("3").html("aaaaaaa");
//$("li").first().html("aaaaaaa");
//$("li").last().html("aaaaaaa");
}

function testHasClass() {
console.log($("#myDiv").hasClass("c1"))
}

function testChildren() {
//如果遍历数组,获取数组中的元素,返回的是一个js对象
console.log($("ul").children().length)
var cs = $("ul").children();
for (var i = 0; i < cs.length; i++) {
console.log(cs[i].innerText);
}
}

function testNext() {
$("p").next().text("ababababa");
}

function testNextAll() {
$("div[id=id1]").nextAll().text("ababababa");
}

function testParent(){
console.log($("p").parent());
}

function testAppend(){
//在#div2的内部 后面追加元素
$("#div2").append("<i>world</i>");
}

function testAppendTo(){
$("<i>world</i>").appendTo("#div2");
}

function testPrepend(){
//在#div2的内部 前面追加元素
$("#div2").prepend("<i>world</i>");
}
function testPrependTo(){
$("<i>world</i>").prependTo("#div2");
}

function testAfter(){
//在#div2的外部 后面追加元素
$("#div2").after("<div><i>world</i><div>");
}

function testInsertAfter(){
$("<div><i>world</i><div>").insertAfter("#div2");
}

function testBefore(){
//在#div2的外部 前面追加元素
$("#div2").before("<div><i>world</i><div>");
}

function testInsertBefore(){
$("<div><i>world</i><div>").insertBefore("#div2");
}

function testWrap(){
$("p").wrap("<div class='wrap'></div>")
}

function testWrapAll(){
$(".aaa").wrapAll("<div class='wrap'></div>")
}

function testReplaceWith(){
$("p").replaceWith("<b>Paragraph. </b>");
}

function testReplaceAll(){
$("<b>Paragraph. </b>").replaceAll("p");
}

function testUnWrap(){
$(".aaa:first").unwrap()
}

function testEmpty(){
$("#clear").empty();
}

function testRemove(){
$("#clear").remove();
}

 

$(document).ready(function(){
console.log("就绪...")
})

//页面载入就绪时,执行一个方法
$(function(){
console.log("就绪2...")
})
$(function(){
console.log("就绪3...")
})


function testBind(){
$("#eveid").bind("focus", function(){
console.log("获取焦点")
})
$("#eveid").bind("focus", function(){
console.log("获取焦点,haha")
})

$("#eveid").bind("blur",function(){
console.log("丢失焦点!");
})

}

function testUnBind(){
$("#eveid").unbind("focus");
}

//一次性事件
function testOne(){
$("#eveid").one("focus",function(){
console.log("hello!");
});
}

function testClick(){
$("#btnid").click(function(){
console.log("我被点击了!!!")
});

$("#btnid").bind("click",function(){
console.log("我被点击了!!!")
});
}

 

function testCss() {
console.log($("#myDiv").css("color"));

$("#myDiv").css("color", "white");
$("#myDiv").css("background-color", "blue");

$("#myDiv").css({
color : "red",
background : "blue"
});

}

function testToggle() {
$("#myDiv").fadeToggle(1000);
}

function testAnimate() {
$("p").animate({
left : "+=50px",
opacity : 'show'
}, 5000);
}

$(function(){
$("#right").click(function(){
$(".block").animate({left: '+50px'}, "slow");
});

$("#left").click(function(){
$(".block").animate({left: '-50px'}, "slow");
});
})

$(function(){
$( "#clickme" ).click(function() {
$( "#book" ).animate({
opacity: 0.25,
left: "+=50",
height: "toggle"
}, 5000, function() {
// 动画完成。
});
});
})

//async:是否异步请求
//data:发送到服务器的数据
//dataType:预期服务器返回数据的类型
function testAjax() {
$.ajax({
url:"hiservlet",
type:"get",
async:true,
data:{aaa:"bbb",ccc:"ddd",fav:"1",fav:"2"}, //"aaa=bbb&ccc=ddd"
dataType:"json",
success:function(data){
console.log(data.aaa);
}
})

$("#results").append( "<tt>" + $("form").serialize() + "</tt>" );
}

 


转载于:https://www.cnblogs.com/moyulove/p/10883275.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值