zoukankan      html  css  js  c++  java
  • 删除指定的元素

    <!doctype html>

    <html lang="en">

    <head>

    <meta charset="UTF-8" />

    <title>删除li</title>

    <style type="text/css">

        *{margin: 0;padding: 0;}

    #box{

    width: 818px;

    height: 494px;

    list-style: none;

    margin: 30px auto;

    }

    #box li{

    background: red;

    width: 100px;

    height: 50px;

    margin-top: 2px;

    margin-right: 2px;

    font-size: 20px;

    color: white;

    text-align: center;

    line-height: 50px;

    float: left;

    }

    #p{

    width: 400px;

    margin: 30px auto;

    }

    </style>

    </head>

    <body>

    <div id="p">

    需要删除数字为哪一个的红方块

    <input type="text" name="" id="content" value="" />

    <input type="button" name="btn" id="btn" value="提交" />

    </div>

    <ul id="box">

     

    </ul>

    </body>

    </html>

    <script type="text/javascript">

    var box = document.getElementById('box');

     

    var content = document.getElementById('content');

    function randFun(){

    return parseInt(Math.random()*9+1);

    }

    for(var i = 0;i < 64;i++){

    var li = document.createElement('li');

    box.appendChild(li);

    li.innerHTML = randFun();

    }

     

    btn.onclick = function(){

    var lis = box.getElementsByTagName('li');

    console.log(lis)

    for(var j = 0;j < lis.length;j++){

        if(lis[j].innerHTML == content.value){

        lis[j].parentNode.removeChild(lis[j]);

        j--;//重点,删除之后,需要减一确保顺序错误

        }

        }

    }

     

     

     

    </script>

  • 相关阅读:
    nvidia-smi电源显示ERR (Pwr:Usage ERR)
    阿里云windows安装ftp
    ansible常用模块
    ansible playbook
    ansible Inventory
    ansible安装
    ansible命令
    ansible配置文件
    js插件中提示框含有 或者<br/>显示不成换行怎么办,改样式
    [转] react项目安装及运行
  • 原文地址:https://www.cnblogs.com/lxlin/p/6017424.html
Copyright © 2011-2022 走看看