操作标签文本

innerHtml 操作双标签中间的HTML

innerText 操作双标签中间的 Text

value 操作表单标签值

<!DOCTYPE html>
<html>
        <head>
                <meta charset="UTF-8">
                <title></title>
                <style>
                        div{
                                border: 1px solid red;
                                width: 200px;
                                height: 200px;
                        }
                </style>
                <script>
                        function fun1(){
                                var element1=document.getElementById("d1");
                                
                                /*
                                 * innerText  不包含HTML结构
                                 * innerHTML  包含HTML结构
                                 * */
                                
                                console.log("innerText>>>"+element1.innerText);
                                console.log("innerHTML>>>"+element1.innerHTML);
                                
                                
                                var element2=document.getElementById("i1");
                                console.log(element2.value)
                        }
                        function fun2(){
                                var element1=document.getElementById("d1");
                                //element1.innerText="<h1>一刻也不能分割</h1>"
                                element1.innerHTML="<h1>一刻也不能分割</h1>"
                                
                                var element2=document.getElementById("i1");
                                element2.value="无论我走到哪里";
                                
                        }
                </script>
        </head>
        <body>
                <div id='d1'>
                        a
                        <span>文字</span>
                        b
                </div>
                
                <input type="text" value="我和我的祖国" id='i1' />
                
                <input type="button" value="获取内容"  onclick="fun1()"/>
                <input type="button" value="修改内容"  onclick="fun2()"/>
        </body>
</html>

增加删除节点

创建元素createElement()

增加元素appendChild()

删除元素removeChild()

<!DOCTYPE html>
<html>
        <head>
                <meta charset="utf-8">
                <title></title>
                <style>
                        #d1{
                                border: 1px solid red;
                                width: 80%;
                                height: 200px;
                        }
                </style>
                <script>
                        function fun1(){
                                var div1=document.getElementById("d1");
                                // 通过JS创建标签
                                var in1=document.createElement("input");
                                in1.setAttribute("type","text");
                                in1.setAttribute("value","请输入内容");
                                
                                var in2=document.createElement("input");
                                in2.setAttribute("type","password");
                                in2.setAttribute("value","123456789");
                                
                                var in3=document.createElement("input");
                                in3.setAttribute("type","button");
                                in3.setAttribute("value","删除");
                                
                                var br=document.createElement("br");
                                
                                in3.onclick=function (){
                                        div1.removeChild(in1)
                                        div1.removeChild(in2)
                                        div1.removeChild(in3)
                                        div1.removeChild(br)
                                }
                                div1.appendChild(in1);
                                div1.appendChild(in2);
                                div1.appendChild(in3);
                                div1.appendChild(br);
                        }
                </script>
        </head>
        <body>
                <div id="d1">
                        
                </div>
                <input type="button" value="增加" onclick="fun1()" />
        </body>
</html>

案例开发

<!DOCTYPE html>
<html>
        <head>
                <meta charset="utf-8" />
                <title></title>
         		<script>
         			function fun1(){
         				var outerDiv =document.getElementById("outerDiv"); 
         				// 1000
         				var left =Math.floor(Math.random()*1000)
         				// 500
         				var top =Math.floor(Math.random()*500)
         				
         				
         				outerDiv.style.marginTop=top+"px";
         				outerDiv.style.marginLeft=left+"px";
         			}
         			
         			function  fun2(){
         				alert("love you too")
         			}
         			
         		</script>
                <style>
                        #outerDiv{
                                width: 200px;
                                height: 200px;
                                border: 1px solid gold;
                                background-color: gray;
                                text-align: center;
                                margin-top: 200px;
                                margin-left: 200px;
                        }
                        #outerDiv input{
                                width: 50px;
                                height: 50px;
                                margin: 20px;
                        }
                </style>
        </head>
        <body >
                <div id="outerDiv">
                        <h3>do you love me?</h3>
                        <input type="button" value="是" onclick="fun2()"/>
                        <input type="button" value="否" onmouseover="fun1()"/>
                </div>
        </body>
</html>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值