- $(document).ready(function() {
- // 页面加载时执行
- //***取得标签的方法$("a")
- //取得所有标签,并且循环给每个标签添加事件.
- $("a").click(function() {
- alert("Hello world!");
- });
- //***在ID前加#,$("#id")
- //***addClass("red");
- //通过ID取得元素,JS方法是document.getElementById("orderedlist")
- //JQuery方法是$("#id"),添加css类的方法addClass("")
- $("#orderedlist").addClass("red");
- //***取得子节点#id > li 或#id > #childID
- //取得子节点的方法 >
- $("#orderedlist > li").addClass("blue");
- //***removeClass("");删除样式参数是css类,如果removeClass()代表删除全部
- //***hover(fun1,fun2)第一个是鼠标经过,第二个是鼠标移开.???
- //***$(this)引用当前对象.
- //***li:last取得最后一个.
- //***每个onXXX()方法都对应有xxx()方法.
- //添加鼠标经过离开的样式.last表示最后一个.
- //通过$(this)引用当前对象.
- //删除样式的方法removeClass("");
- $("#orderedlist li:last").hover(function() {
- $(this).addClass("green");
- }, function() {
- $(this).removeClass("green");//删除所有removeClass()
- });
- //***html()用来获取每个li的html文本, 追加一些文字,并将之设置为li的html文本。
- //***.html('xxx')是设置'xxx'为对象的html代码
- $("#orderedlist").find("li").each(function(i) {
- $(this).html( $(this).html() + " BAM! " + i );
- });
- //***find()方法同$("#test1 li")一样,找到所有的标签.
- //***each()类似foreach(),迭代所有项,(i)直接用.
- $("#test1").find("li").each(function(i) {
- $(this).html( $(this).html() + " BAM! " + i );
- });
- });
- /***
- $("#reset").click(function(){
- $("#form")[0].reset();//选择所有id为form的元素,[0]表示第一个。reset()是form的方法。
- });***/
- //***为所有form执行reset
- $("#reset").click(function() {
- $("form").each(function() {
- this.reset();
- });
- });
- //***fiter,not过滤,css("样式名","样式值");
- /*
- 你可能要面对的问题是不希望某些特定的元素被选择。jQuery 提供了filter() 和not() 方法来解决这个问题。
- filter()以过滤表达式来减少不符合的被选择项,
- not()则用来取消所有符合过滤表达式的被选择项.
- 考虑一个无序的list,你想要选择所有的没有ul子元素的li元素。
- */
- $("li").not("[ul]").css("border", "1px solid black").css("color","red");//css也可以链式操作
- /*
- 这个代码选择了所有的li元素,然后去除了没有ul子元素的li元素。
- 刷新浏览器后,所有的li元素都有了一个边框,只有ul子元素的那个li元素例外。
- */
- //选择所有的带有name属性的链接:
- $("a[@name]").background("red");
- //081106
- /**<a id="hrefTest" href="gv/hello/lll">特定href</a>
- 注意这里的方式是a[@href *= ]这里是部分匹配。
- */
- $("a[@href*=gv/hello]").click(function() {
- alert('特定href');
- });
- /*hide()隐藏匹配对象。
- end()结束对 “dd” 的引用而返回到 “#faq ”
- next()寻找同辈元素,紧邻的。可是这里明显是子辈不是同辈,奇怪。
- slideUp()/Down()向上滑动隐藏或向下滑动展开。
- */
- $('#faq').find('dd').hide().end().find('dt').click(function() {
- var answer = $(this).next();
- if (answer.is(':visible')) {
- answer.slideUp();
- } else {
- answer.slideDown();
- }
- });
- /*用 $(this).next() 来找到dt下面紧接的一个dd元素
- answer.is(':visible')
- 这里的highlight类,即给这个段落加个边框。
- */
- $("a").hover(function() {
- $(this).parents("p").addClass("highlight");
- }, function() {
- $(this).parents("p").removeClass("highlight");
- });