JavaScript高级程序成绩(第四版)笔记 DOM-Document类型定位元素 文档写入

在这里插入图片描述

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
    <div  id="id1">
      <h1  id="id4">123</h1>
        <script type="text/javascript">
            document.write("<strong>"+(new Date()).toString()+"</strong>");
        </script>
    </div>
    <div id="id2">
      <h1 style="color:red">标题</h1>
      <h1>标题</h1>
        <img name="name1" src="img1.jpg" width="200" height="200">
        <a href="index.html">链接</a>
        <a href="js01Bom.html" name="nameA">链接2</a>


    </div>
    <div id="id3">
        <fieldset>
            <legend>Which color do you prefer?</legend>
             <ul>
            <li>
                <input type="radio" value="red" name="color" id="colorRed">
                <label for="colorRed">Red</label>
            </li>
            <li>
                <input type="radio" value="green" name="color" id="colorGreen">
                <label for="colorGreen">Green</label>
            </li>
            <li>
                <input type="radio" value="blue" name="color" id="ColorBlue">
                <label for="ColorBlue">Blue</label>
            </li>
        </ul>
        </fieldset>

    </div>
<!--
<script type="text/javascript">
    window.onload = function(){
        document.write("Hello world");
    }
</script>
-->
</body>
<script src="js04Document.js"></script>
</html>
//定位元素
//document.getElementById()  document.getElementByTagName()

let element1 = document.getElementById('id1');
console.log(element1.nodeName);//div

let listElement= document.getElementsByTagName('h1');
console.log(listElement);//HTMLCollection(3)[h1#id4, h1, h1, id4: h1#id4]

console.log(listElement[1].style.color);

//namedItem 通过标签的name属性获得某一项的引用
let ImgName = document.images.namedItem('name1');
console.log(ImgName);
//中括号获取
let imgName2= document.images['name1'];
console.log(imgName2);

//获得所有元素,传入*
console.log(document.getElementsByTagName("*"));
//HTMLCollection(13)[html, head, meta, title, body, div#id1, h1#id4, div#id2, h1, h1, img, div#id3, script, id1: div#id1, id4: h1#id4, id2: div#id2, name1: img, id3: div#id3]

//getElementByName,常用在单选按钮
let radios =document.getElementsByName('color');
console.log(radios);

//特殊集合
console.log(document.anchors);//包含文档中所有带name的<a>元素
console.log(document.applets)//已废弃,规定java applet的文件名
console.log(document.forms);//所有form元素
console.log(document.images);//img元素
console.log(document.links);//包含文档中所有带href属性的<a>元素

let hasCore = document.implementation.hasFeature('Core',"1.0");//true
let hasEvent = document.implementation.hasFeature("Events","4.0")
console.log(hasCore,hasEvent);
//implementation.hasFeature 无论写什么都是true ,已废弃

//文档写入
//write writeln()
document.write('abc');
document.writeln('abcd');

又写了个用document.createElement()插入图片的练习

let newElementImg = document.createElement('img');
newElementImg.src='img1.jpg';
newElementImg.id='id3';
newElementImg.width=200;
newElementImg.height=200;
let newElementDiv = document.createElement('div');
newElementDiv.id='id4';
document.body.appendChild(newElementDiv);
newElementDiv.appendChild(newElementImg);
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值