<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title></title>
<!--
使ie以IE8的模式运行
-->
<meta http-equiv="X-UA-Compatible" content="IE=EmulateIE8" >
<script type="text/javascript"> /**
* 得到页面元素节点的几种方式
*/ //通过id选择器去得到
function getDom01() {
//获取一个
var div = document.getElementById("box1");
console.log(div);
} //通过标签名得到元素
function getDom02(){
//会得到所有的 与名字相同的标签
var divs = document.getElementsByTagName("div");
console.log(divs[0]);
}
//根据名字得到
function getDom03(){
//只能得到 原生就有name属性的元素 不能是自定义的
var div = document.getElementsByName("box2");
document.getElementsByClassName("box1");
console.log(div[0]);
}
//IE7 以及以前的版本 不支持
function getDom04(){
var div = document.querySelector("#box1");
console.log(div);
}
//IE7 以及以前的版本 不支持
function getDom05(){
var span = document.querySelectorAll(".box > span");
console.log(span.length);
}
</script>
<style type="text/css">
.box{
border: 1px solid red;
width: 450px;
height: 100px;
} .box span{
border-left: 1px solid green;
border-right: 1px solid green;
margin: 0 10px;
padding: 0 5px;
}
</style>
</head>
<body>
<input type="button" onclick="getDom01()" value="getDom01"/>
<input type="button" onclick="getDom02()" value="getDom02"/>
<input type="button" onclick="getDom03()" value="getDom03" name="box2"/>
<input type="button" onclick="getDom04()" value="getDom04" name="box2"/>
<input type="button" onclick="getDom05()" value="getDom05" name="box2"/>
<hr/>
<div class="box" id="box1" name="box2">
<span>this is a span in div</span>
<span>this is a span in div</span>
<span>this is a span in div</span>
<span>this is a span in div</span>
</div>
</body>
</html>

js中获取页面元素节点的几种方式的更多相关文章

  1. 在js中获取页面元素的属性值时,弱类型导致的诡异事件踩坑记录,

    前几天写一个js的时候遇到一个非常诡异的事情,这个问题是这样的,我要获取一个页面的DOM元素的val值,判断这个值是否比某个变量大,这个需求原先数字最大也就是10,现在要改了,可能会更多,这个时候我发 ...

  2. Struts2中获取HttpServletRequest,HttpSession等的几种方式

    转自:http://www.kaifajie.cn/struts/8944.html package com.log; import java.io.IOException; import java. ...

  3. Action 中获取表单数据的三种方式

    (尊重劳动成果,转载请注明出处:http://blog.csdn.net/qq_25827845/article/details/53138905  冷血之心的博客) Action 中获取表单提交数据 ...

  4. 示例 - 10行代码在C#中获取页面元素布局信息

    最近研究一个如何在网页定位验证码并截图的问题时, 用SS写了一段C#小脚本可以轻松获取页面任意元素的布局信息 (top, left, width, height). 10行功能代码, 觉得有点用, 现 ...

  5. Day20-单表中获取表单数据的3种方式

    1. 搭建环境请参考:http://www.cnblogs.com/momo8238/p/7508677.html 2. 创建表结构 models.py from django.db import m ...

  6. JS 中对变量类型判断的几种方式

    文章整理搬运,出处不详,如有侵犯,请联系~   数据类型判断和数据类型转换代码工具 在 JS 中,有 5 种基本数据类型和 1 种复杂数据类型,基本数据类型有:Undefined, Null, Boo ...

  7. Activity启动过程中获取组件宽高的五种方式

    第一种:(重写Activity的onWindowFocusChanged方法) /** * 重写Acitivty的onWindowFocusChanged方法 */ @Override public ...

  8. web项目中实现页面跳转的两种方式

    <a href="javascript:"></a>跳转在网页本身,URL不改变 <a href="#"></a> ...

  9. JS中获取页面单选框radio和复选框checkbox中当前选中的值

    单选框:单选框的name值全部相同 页面有一组单选框的元素<td><input type="radio name="radioid">满意< ...

随机推荐

  1. 解决MySQL workbench的Can't connect to MySQL server on '127.0.0.1'(10061)问题

    如题,今天打开MySQL时,出现了这种问题,无法连接到数据库 问题原因:The error (2003) Can't connect to MySQL server on 'server' (1006 ...

  2. js遍历json的key和value

    遍历json对象: 无规律: <script> var json = [{dd:'SB',AA:'东东',re1:123},{cccc:'dd',lk:'1qw'}]; for(var i ...

  3. ios系统降级

    1.使用PP助手/iTunes备份好文件资料,以防重要信息丢失: 2.设备连接iTunes,按住Shift键之后点击“恢复iPhone”,选择已下载好的iOS8.4.1固件,等待更新完成即可. 注意要 ...

  4. tensorflow中 tf.train.slice_input_producer 和 tf.train.batch 函数

    tensorflow数据读取机制 tensorflow中为了充分利用GPU,减少GPU等待数据的空闲时间,使用了两个线程分别执行数据读入和数据计算. 具体来说就是使用一个线程源源不断的将硬盘中的图片数 ...

  5. [CF125E]MST Company

    codeforces description 给出一张\(n\)点\(m\)条边的无向图,求一棵满足\(1\)号点度数恰好为\(k\)的最小生成树,并输出方案. \(1\le k\le n\le500 ...

  6. [LOJ2541]「PKUWC2018」猎人杀

    loj description 有\(n\)个猎人,每个猎人有一个仇恨度\(w_i\),每个猎人死后会开一枪打死一个还活着的猎人,打中每个猎人的概率与他的仇恨度成正比. 现在你开了第一枪,打死每个猎人 ...

  7. Struts2自定义标签4自定义分页标签

    第一步:webroot/web-inf下的str.tld文件 <?xml version="1.0" encoding="UTF-8"?> < ...

  8. HDU1735 字数统计

    版权声明:长风原创 https://blog.csdn.net/u012846486/article/details/28011667 字数统计 Time Limit: 1000/2000 MS (J ...

  9. 【转】Notepad++中Windows,Unix,Mac三种格式之间的转换

    原文网址:http://www.crifan.com/files/doc/docbook/rec_soft_npp/release/htmls/npp_func_windows_unix_mac.ht ...

  10. 负载均衡-haproxy安装配置

    HAProxy提供高可用性.负载均衡以及基于TCP和HTTP应用的代理,支持虚拟主机,它是免费.快速并且可靠的一种解决方案.HAProxy特别适用于那些负载特大的web站点,这些站点通常又需要会话保持 ...