DOM

课程任务

题目1: dom对象的innerText和innerHTML有什么区别?

答:innerText获取的是dom对象内的文本内容,inner HTML获取的是dom对象内的元素结构。

题目2: elem.children和elem.childNodes的区别?

答:elem.children只返回HTML元素节点,不包括文本,elem.childNodes返回元素节点和文本以及所有属性节点。一般用children。

题目3:查询元素有几种常见的方法?ES5的元素选择方法是什么?

答:document.getElementById('id'),document.getElementsByClassName('className'),document.getElementsByTagName('TagName'),document.getElementsByName('Name').ES5 的元素选择方法是document.querySelector('#id .className' 'TagName'等CSS选择器),document.querySelectorAll('#id .className' 'TagName'等CSS选择器),elementFromPoint方法返回位于页面指定位置的元素。var element = document.elementFromPoint(x, y);

题目4:如何创建一个元素?如何给元素设置属性?如何删除属性

答:创建一个元素:document.createElement('div') 元素设置属性,elem.setAttribute('myAttribute','myAttributeValue')或者var myAttribute =document.createAttribute('myAttribute'); myAttribute.value ='myAttributeValue';var node =document.getElementById('id');node.setAttribute(myAttribute);删除属性:elem.removeAttribute('myAttribute')。上面方法也可以直接操作元素属性来实现,elem.Attribute = ''

题目5:如何给页面元素添加子元素?如何删除页面元素下的子元素?

答:
<pre>var newElem =document.createElement('div')
var text =document.createTextNode('新创建的段落')
newElem.appendChild(text)
var pElem =document.querySelector('#a')
pElem.appendChild(newElem)
//五秒后删除这个p下面的子元素
setTimeout('yourFunction()',5000);
function yourFunction() {
pElem.removeChild(newElem)
}</pre>

题目6: element.classList有哪些方法?如何判断一个元素的 class 列表中是包含某个 class?如何添加一个class?如何删除一个class?

答:add('anotherclass'),添加指定的类值。remove( String [,String] )
删除指定的类值。item(Number),按集合中的索引返回类值。toggle ( String [, force] )
当只有一个参数时:切换 class value; 即如果类存在,则删除它并返回false,如果不存在,则添加它并返回true。当存在第二个参数时:如果第二个参数的计算结果为true,则添加指定的类值,如果计算结果为false,则删除它。contains( String )
检查元素的类属性中是否存在指定的类值。
<pre>
ele[0].classList.add("anotherclass");
undefined
ele[0].classList.remove("anotherclass");
undefined
ele[0].classList.item
function item() { [native code] }
ele[0].classList.item(0)
"mod-tabs"
ele[0].classList.item(1)
null
ele[0].classList.add("anotherclass");
undefined
ele[0].classList.item(1)
"anotherclass"
ele[0].classList.toggle('anotherclass')
false
ele[0].classList.toggle('anotherclass')
true
ele[0].classList.toggle('anotherclass')
false
ele[0].classList.item(1)
null
ele[0].classList.toggle('anotherclass')
true
ele[0].classList.item(1)
"anotherclass"
</pre>

题目7: 如何选中如下代码所有的li元素? 如何选中btn元素?

    <div class="mod-tabs">
       <ul>
           <li>list1<li>
           <li>list2<li>
           <li>list3<li>
       </ul>
       <button class="btn">点我</button>
</div>
<srcipt>
    //选中所有的li元素
    var liAll =document.querySelectorAll('.mod-tabs li')
    //选中btn元素
    var btn =document.querySelector('.btn')
</srcipt>

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 基本介绍 文档对象模型 (DOM) 是HTML和XML文档的编程接口。它给文档(结构树)提供了一个结构化的表述并且...
    草鞋弟阅读 3,437评论 0 0
  • 问答 一、dom对象的innerText和innerHTML有什么区别? innerTextinnerText是一...
    婷楼沐熙阅读 3,146评论 0 0
  • 题目1: dom对象的innerText和innerHTML有什么区别? innerText是一个可写属性,返回元...
    QQQQQCY阅读 1,589评论 0 0
  • 题目1: dom对象的innerText和innerHTML有什么区别? innerText是一个可写属性,返回元...
    Taaaaaaaurus阅读 1,603评论 0 1
  • 由于中国人生活习惯和传统文化的原因,多数人都认为煲汤喝是一种非常滋补的养生方法。不少人喜欢在身体虚弱、需要"进补"...
    吕艳朋阅读 5,752评论 5 49

友情链接更多精彩内容