javascript,迷迷糊糊的专二,难受的第六天,部分作业的格式

字符串去除空格倒叙

var arr="hj hjj hhh jjj lll ";
var num=0;
for(var i=0;i

带返回值的函数

function fn(m, n) {
    if (typeof m != "number" || n != "number") {
        alert("请输入正确答案");
        return;  //可以终止函数执行
    }
    var newArr = [];
    for (i = m; i <= n; i++) {
    //该数字是5的倍数或者各位包括3或者十位数包含3
        if (i % 5 == 0 || i % 10 == 3 || parseInt(i / 10) == 3) {
            newArr.push(i)
        }
    }
    return newArr;
}
var x = fn(2, 21);
document.write(x)

计算器的格式

var oTxt1=document.getElementById("txt1");
var oTxt2=document.getElementById("txt2");
var oTxt3=document.getElementById("txt3");
//出来的变量是字符串类型
function jisuan(){
    oTxt3.value=parseInt(oTxt1.value)+parseInt(oTxt2.value);
}

摘取数组中的不同

var arr = ["李白", 123, "杜甫", "王维", 45, 657, "李清照",];
function fn(arr) {
    var arrNew = [];
    for (var i = 0; i < arr.length; i++) {
        if (typeof arr[i] == 'string') {
            arrNew.push(arr[i]);
        }
    }

    for (var j = 0; j < arrNew.length; j++) {
        document.write('著名诗人:' + arrNew[j] + "
") } } fn(arr);

九九乘法表

document.write('')
for (var a = 1; a <= 9; a++) {
    document.write('');
    for (var b = 1; b <= a; b++) {
    document.write('')
    }
    document.write('');
}
document.write('
' + b + 'x' + a + '=' + a * b + '
')

照片的顺序生成

var oImg = document.getElementById("img");
function fn() {
    var num = 0;
    setInterval(function () {
        var oImgs = ["../imgs/1.jpg", "../imgs/2.jpg", "../imgs/3.jpg", "../imgs/4.jpg", "../imgs/5.jpg", "../imgs/6.jpg", "../imgs/7.jpg", "../imgs/8.jpg", "../imgs/9.jpg", "../imgs/10.jpg", "../imgs/11.jpg", "../imgs/12.jpg"]
        oImg.src = oImgs[num++];
        if (num == 12) {
            num = 0;
        }
    }, 1000)
}

录入身份信息

var oTxt = document.getElementById("txt");
var oTbody = document.getElementById("body");
var num = 1;
var oIpt = document.getElementsByClassName("chk");
var oNan = document.getElementById("nan");
var oNv = document.getElementById("nv");
var oTrs = document.getElementsByTagName("tr");
function tianj() {
    if (oTxt.value == "" || oTxt.value == " ") {
        alert("内容为空不能发表");
        return false;
    }
    var oTr = document.createElement("tr");
    var oTd1 = document.createElement("td");
    var oTd2 = document.createElement("td");
    var oTd3 = document.createElement("td");
    var oTd4 = document.createElement("td");
    var oTd5 = document.createElement("td");
    var oTd6 = document.createElement("td");
    var oTd7 = document.createElement("td");

    oTd1.innerHTML = oTxt.value;
    oTd2.innerHTML = '';
    // oTd3.innerHTML = "东方不败";

    if (oNan.checked == true) {
        oTd3.innerHTML = "男"
    }
    if (oNv.checked == true) {
        oTd3.innerHTML = "女"
    }
    // oTd4.innerHTML =      //'负一楼';
  
    oTd5.innerHTML = gRi();
    oTd6.innerHTML = '';
    oTd7.innerHTML = '';

    if (num == 4) {
        num = 1
    }

    oTr.appendChild(oTd1);
    oTr.appendChild(oTd2);
    oTr.appendChild(oTd3);
    oTr.appendChild(oTd4);
    oTr.appendChild(oTd5);
    oTr.appendChild(oTd6);
    oTr.appendChild(oTd7);

    oTbody.appendChild(oTr);
    // oTbody.insertBefore(oTr,oTbody.firstChild);

    oTxt.value = "";
    louc(); 
}
//获取时间格式,自己设定
function gRi() {
    var date = new Date();
    var y = date.getFullYear();
    var m = date.getMonth() + 1;
    var d = date.getDate();

    var h = date.getHours();
    var mm = date.getMinutes();
    var s = date.getSeconds();

    h = h < 10 ? "0" + h : h;
    mm = mm < 10 ? "0" + mm : mm;
    s = s < 10 ? "0" + s : s;

    return y + "-" + m + "-" + d + " " + h + ":" + mm + ":" + s;
}
//删除自己和删除上级父元素
function shanc(obj) {
    obj.parentNode.parentNode.remove();
    louc();
}
//全选和取消全选
function qx() {
    for (i = 0; i < oIpt.length; i++) {
        oIpt[i].checked = !oIpt[i].checked
    }
}
//设置楼层的加减
function louc() {
    for (var i = 1; i < oTrs.length; i++) {
        oTrs[i].children[3].innerHTML = i + "楼"
    }
}

全选框,取消全选框

var oChk = document.getElementsByName("chk");
function fx() {
    for (i = 0; i < oChk.length; i++) {
        oChk[i].checked = !oChk[i].checked
    }
}

克隆的生成

var oBox=document.getElementById("box");
var oDiv=document.getElementsByClassName("xiao")[0];
function kl(){
    var oKl=oDiv.cloneNode(true);
    oBox.appendChild(oKl)
}

点名器的生成

var oBox = document.getElementById("box");
// var oIpt = document.getElementById("txt");
var qJies;
function kais() {
    qJies = setInterval(function () {
        clearInterval();
        var arr = ["讲师", "赵龙", "许亚阁", "陈俊杰", "吴守佳", "岳欣", "申慧聪", "宋珂新", "陈雪莲", "王纪清", "赵宇",
            "王家祥", "张耀木", "刘立奇", "高坤", "陶刘园", "杨峥", "杨进位", "徐建浩", "冯俊豪", "马全旺", "刘洋", "付政法", "孙红雨",
            "杨宏宇", "杜肖杰", "王润雨", "周小丽", "王世成", "杜世纪", "梁金钊"];
        var num = Math.floor(Math.random() * arr.length);
        oBox.innerHTML = arr[num]
    }, 100)
}
function jies(){
    clearInterval(qJies);
}

生成验证码,验证的通过

var oBox =document.getElementById("box");
    //定义一个字符串
var str="qwertyuiopasdfghjklzxcvbnm1234567890";
    //定义一个空字符串,用于拼接四位字符
var ss="";
    //for循环遍历四次,产生四个字符,并拼接今空字符串中
for(var i=1;i<=4;i++){
    var num=Math.floor(Math.random()*str.length);
    ss+=str[num];    //拼接该随机下标对应的字符
}
    //获取到ID为box的div
var oBox=document.getElementById("box");
    //获取到ID位TXT的输入框
var oTxt=document.getElementById("txt");
    //把你产生四位数验证码放入 DIV标签
oBox.innerHTML=ss;
    //定义函数,点击按钮时调用
function fn(){
    //如果文本框的内容和DIV的内容一样,就弹出成功,否则就弹出失败
    if(oTxt.value==oBox.innerHTML){
        alert("验证成功");
    }else{
        alert("验证失败");
    }
}

随机选取图片

var oImg = document.getElementById("img");
var qc;
function kais() {
    qc=setInterval(function () {
        var arr = ["../img/aaa/1.jpg", "../img/aaa/2.jpg", "../img/aaa/3.jpg", "../img/aaa/4.jpg", "../img/aaa/5.jpg", "../img/aaa/6.jpg", "../img/aaa/7.jpg", "../img/aaa/8.jpg", "../img/aaa/9.jpg", "../img/aaa/10.jpg", "../img/aaa/11.jpg", "../img/aaa/12.jpg", "../img/aaa/13.jpg", "../img/aaa/14.jpg", "../img/aaa/15.jpg", "../img/aaa/16.jpg", "../img/aaa/17.jpg", "../img/aaa/18.jpg", "../img/aaa/19.jpg", "../img/aaa/20.jpg", "../img/aaa/21.jpg", "../img/aaa/22.jpg", "../img/aaa/23.jpg", "../img/aaa/24.jpg", "../img/aaa/25.jpg", "../img/aaa/26.jpg", "../img/aaa/27.jpg"]
        var suij = Math.floor(Math.random() * arr.length)
        oImg.src = arr[suij]
    }, 1000)
}
function jies() {
    clearInterval(qc);
}

随机座位的选取

// var oBox = document.getElementsByClassName("xiao");
// //  在arr数组中存入50个不重复的数字 范围1-50
// var arr = [];
//     // 往数组中存入50个不重复的数字
// for (var i = 1; true; i++) {
//     var num = Math.floor(Math.random() * (50 - 1 + 1) + 1);
//     if (arr.indexOf(num) == -1) {
//         arr.push(num);
//     }
//     if (arr.length == 50) {
//         break;
//     }
// }
//     // arr中含有50个不重复的数字  放在盒子里面
// for (var i = 0; i < arr.length; i++) {
//     oBox[i].innerHTML = arr[i];
// }

留自己排除其他

//6....排他思想
var oIpt = document.getElementsByTagName("input");
for (i = 0; i < oIpt.length; i++) {

    oIpt[i].onclick = function () {

        for (j = 0; j < oIpt.length; j++) {
            oIpt[j].style.color = "";
            oIpt[j].style.background = "";
            oIpt[j].value = "未点";
        }

        this.style.color = "red";
        this.style.background = "blue";
        this.value = "已点"
    }
}

你可能感兴趣的:(javascript,迷迷糊糊的专二,难受的第六天,部分作业的格式)