Jquery学习总结

入门文章:

http://www.k99k.com/jQuery_getting_started.html

$(document).ready(function() {

$("a").click(function() {

alert("Hello world!");

});

});

这样在你点击页面的一个链接时都会触发这个"Hello world"的提示。

$("a") 是一个jQuery选择器(selector),在这里,它选择所有的a标签(即<a></a>)

$("#orderedlist")等价于document.getElementById("orderedlist")

$("#orderedlist").addClass("red");

为id为orderedlist的节点添加red样式类 。

$('.poem-stanza').addClass("red");

为class为poem-stanza的所有元素添加red样式类 。

$(document).ready(function() {

$("#orderedlist").find("li").each(function(i) {

$(this).html( $(this).html() + " BAM! " + i );

});

});




在页面加载完毕后,遍历id为orderedlist的节点的所有li子节点,




并对每个子节点的Html文本重新赋值。




.html()方法是获取对象的html代码,而.html('xxx')是设置'xxx'为对象的html代码




jQuery的一些特性和用法:




1.精准简单的选择对象(dom):



$('#element');// 相当于document.getElementById("element")

$('.element');//Class
$('p');//html标签
$("form > input");//子对象
$("div,span,p.myClass");//同时选择多种对象
$("tr:odd").css("background-color", "#bbbbff");//表格的隔行背景
$(":input");//表单对象
$("input[name='newsletter']");//特定的表单对象 2.对象函数的应用简单和不限制: element.function(par); $(”p.surprise”).addClass(”ohmy”).show(”slow”)... 3.对已选择对象的操作(包括样式): $("#element").addClass("selected");//给对象添加样式
$('#element').css({ "background-color":"yellow", "font-weight":"bolder" });//改变对象样式
$("p").text("Some new text.");//改变对象文本
$("img").attr({ src: "test.jpg", alt: "Test Image" });//改变对象文本
$("p").add("span");//给对象增加标签
$("p").find("span");//查找对象内部的对应元素
$("p").parent();//对象的父级元素
$("p").append("<b>Hello</b>");//给对象添加内容 4.支持aJax,支持文件格式:xml/html/script/json/jsonp $("#feeds").load("feeds.html");//相应区域导入静态页内容
$("#feeds").load("feeds.php", {limit: 25}, function() {alert("The last 25 entries in the feed have been loaded");});//导入动态内容 4.对事件的支持: $("p").hover(function () {
      $(this).addClass("hilite");//鼠标放上去时
    }, function () {
      $(this).removeClass("hilite");//移开鼠标
    });//鼠标放上去和移开的不同效果(自动循环所有p对象5.动画: $("p").show("slow");//隐藏对象(慢速渐变)
$("#go").click(function(){
$("#block").animate({
    "90%",
    height: "100%",
    fontSize: "10em"
}, 1000 );
});//鼠标点击后宽、高、字体的动态变化 6.扩展: $.fn.background = function(bg){
    return this.css('background', bg);
};
$(#element).background("red"); 如果要为每一个jQuery 对象添加一个函数,必须把该函数指派给 $.fn,同时这个函数必须要返回一个 this(jQuery 对象)







原文地址:https://www.cnblogs.com/vstar/p/1243343.html