/* 2 功能:生成博客目录的JS工具 3 测试:IE8,火狐,google测试通过 6 */ 7 var BlogDirectory = { 8 /* 9 获取元素位置,距浏览器左边界的距离(left)和距浏览器上边界的距离(top) 10 */ 11 getElementPosition:function (ele) { 12 var topPosition = 0; 13 var leftPosition = 0; 14 while (ele){ 15 topPosition += ele.offsetTop; 16 leftPosition += ele.offsetLeft; 17 ele = ele.offsetParent; 18 } 19 return {top:topPosition, left:leftPosition}; 20 }, 21 22 /* 23 获取滚动条当前位置 24 */ 25 getScrollBarPosition:function () { 26 var scrollBarPosition = document.body.scrollTop || document.documentElement.scrollTop; 27 return scrollBarPosition; 28 }, 29 30 /* 31 移动滚动条,finalPos 为目的位置,internal 为移动速度 32 */ 33 moveScrollBar:function(finalpos, interval) { 34 35 //若不支持此方法,则退出 36 if(!window.scrollTo) { 37 return false; 38 } 39 40 //窗体滚动时,禁用鼠标滚轮 41 window.onmousewheel = function(){ 42 return false; 43 }; 44 45 //清除计时 46 if (document.body.movement) { 47 clearTimeout(document.body.movement); 48 } 49 50 var currentpos =BlogDirectory.getScrollBarPosition();//获取滚动条当前位置 51 52 var dist = 0; 53 if (currentpos == finalpos) {//到达预定位置,则解禁鼠标滚轮,并退出 54 window.onmousewheel = function(){ 55 return true; 56 } 57 return true; 58 } 59 if (currentpos < finalpos) {//未到达,则计算下一步所要移动的距离 60 dist = Math.ceil((finalpos - currentpos)/10); 61 currentpos += dist; 62 } 63 if (currentpos > finalpos) { 64 dist = Math.ceil((currentpos - finalpos)/10); 65 currentpos -= dist; 66 } 67 68 var scrTop = BlogDirectory.getScrollBarPosition();//获取滚动条当前位置 69 window.scrollTo(0, currentpos);//移动窗口 70 if(BlogDirectory.getScrollBarPosition() == scrTop)//若已到底部,则解禁鼠标滚轮,并退出 71 { 72 window.onmousewheel = function(){ 73 return true; 74 } 75 return true; 76 } 77 78 //进行下一步移动 79 var repeat = "BlogDirectory.moveScrollBar(" + finalpos + "," + interval + ")"; 80 document.body.movement = setTimeout(repeat, interval); 81 }, 82 83 htmlDecode:function (text){ 84 var temp = document.createElement("div"); 85 temp.innerHTML = text; 86 var output = temp.innerText || temp.textContent; 87 temp = null; 88 return output; 89 }, 90 91 /* 92 创建博客目录, 93 id表示包含博文正文的 div 容器的 id, 94 mt 和 st 分别表示主标题和次级标题的标签名称(如 H2、H3,大写或小写都可以!), 95 interval 表示移动的速度 96 */ 97 createBlogDirectory:function (id, mt, st, interval){ 98 //获取博文正文div容器 99 var elem = document.getElementById(id); 100 if(!elem) return false; 101 //获取div中所有元素结点 102 var nodes = elem.getElementsByTagName("*"); 103 //创建博客目录的div容器 104 var divSideBar = document.createElement('DIV'); 105 divSideBar.className = 'sideBar'; 106 divSideBar.setAttribute('id', 'sideBar'); 107 var divSideBarTab = document.createElement('DIV'); 108 divSideBarTab.setAttribute('id', 'sideBarTab'); 109 divSideBar.appendChild(divSideBarTab); 110 var h2 = document.createElement('H2'); 111 divSideBarTab.appendChild(h2); 112 var txt = document.createTextNode('目录导航'); 113 h2.appendChild(txt); 114 var divSideBarContents = document.createElement('DIV'); 115 divSideBarContents.style.display = 'none'; 116 divSideBarContents.setAttribute('id', 'sideBarContents'); 117 divSideBar.appendChild(divSideBarContents); 118 //创建自定义列表 119 var dlist = document.createElement("dl"); 120 divSideBarContents.appendChild(dlist); 121 var num = 0;//统计找到的mt和st 122 mt = mt.toUpperCase();//转化成大写 123 st = st.toUpperCase();//转化成大写 124 //遍历所有元素结点 125 for(var i=0; i

JQuery学习笔记(2)——数组 属性 事件

each遍历

JQueryObjectArray.each(function(index,Element))

$(".myTable").each(function(i,ele){
    //使用模板函数
    //这里的ele是一个DOM对象,要想使用jQuery对象,可以这样写$(this)
    //function里面的i和ele两个参数,根据实际情况填
    console.log(`${i}: ele.innerText`);
});

toFixed(2) 保留2位小数

数组map拼接

数组调用map,会自动拼接成一个字符串

 $.getJSON('json_data.html', {name1: '参数值', name2: 'value2'}, function(res) { 
        // 服务器响应,返回的json数据
        // es6 数组的map()
        const trArr = res.map(item => {
            return `
                <tr>
                    <td>${item.empno}</td>
                    <td>${item.ename}</td>
                    <td>${item.sal}</td>
                </tr>
            `
        });
        // console.log(...trArr);
        // join()将数组的元素连接成一个字符串
        console.log(trArr.join(''));

       $('#myDiv').html(`
                <table class="table">
                    <tr>
                        <th>编号</th>
                        <th>姓名</th>
                        <th>工资</th>
                    </tr>
                   ${trArr.join('')}
                </table>
            `);
    });
});

获得属性

获得属性有两种方法

  • attr(propertyName)
  • prop(propertyName)
    两者使用区别如下:
  • 对于HTML元素本身就带有的固有属性,在处理时,使用prop方法。
  • 对于HTML元素我们自己自定义的DOM属性,在处理时,使用attr方法
    如果使用prop去获得自定义的属性,会返回undefined(未定义)

设置属性

设置属性也是两种方法,方法名与获得属性的两种方法相同,只不过多了个参数

  • attr(propertyName,value)
  • prop(propertyName,value)

设置全选例子:

<form action="">
    <input type="checkbox" id="checkall" >全选 <br>
    <br>
    爱好:<br>
    <input type="checkbox" name="hobby">读书<br><br>
    <input type="checkbox" name="hobby">电影<br><br>
    <input type="checkbox" name="hobby">游戏<br><br>
    <input type="checkbox" name="hobby">游泳<br><br>
    <input type="checkbox" name="hobby">写代码<br><br>
</form>

<script>
$(function(){
    $('#checkall').click(function(){
        console.log(this);
        if(this.checked){
            $(":input[name='hobby']").attr("checked",true);
        }else{
            $(":input[name='hobby']").attr("checked",false);
        }
        
    })
});
</script>

删除属性

  • removeAttr(attrname)
  • removeAttr(attrname)
$(':button').removeAttr("name");

添加和删除css类

  • addClass()
  • removeClass()

addClass无法实现替换,一般通过删除之后再添加来实现替换class的效果

$("p").removeClass("myClass noClass").addClass("yourClass");

显示和隐藏

  • hide()
  • show()
$('#mydiv').hide();
$('#mydiv').show();

设置事件监听器

//鼠标移入移出
$("#mybutton").hover(function(){
    //这里是鼠标移入后执行的逻辑操作
},function(){
    //这里是鼠标移出后执行的逻辑操作
});
//鼠标点击
$("#mybutton").click(function(){
    //这里是鼠标点击后执行的逻辑操作
});
posted @ 2019-09-19 15:03  我的人生  阅读(205)  评论(0)    收藏  举报