js中获取页面元素节点的几种方式
<!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中获取页面元素节点的几种方式的更多相关文章
- 在js中获取页面元素的属性值时,弱类型导致的诡异事件踩坑记录,
前几天写一个js的时候遇到一个非常诡异的事情,这个问题是这样的,我要获取一个页面的DOM元素的val值,判断这个值是否比某个变量大,这个需求原先数字最大也就是10,现在要改了,可能会更多,这个时候我发 ...
- Struts2中获取HttpServletRequest,HttpSession等的几种方式
转自:http://www.kaifajie.cn/struts/8944.html package com.log; import java.io.IOException; import java. ...
- Action 中获取表单数据的三种方式
(尊重劳动成果,转载请注明出处:http://blog.csdn.net/qq_25827845/article/details/53138905 冷血之心的博客) Action 中获取表单提交数据 ...
- 示例 - 10行代码在C#中获取页面元素布局信息
最近研究一个如何在网页定位验证码并截图的问题时, 用SS写了一段C#小脚本可以轻松获取页面任意元素的布局信息 (top, left, width, height). 10行功能代码, 觉得有点用, 现 ...
- Day20-单表中获取表单数据的3种方式
1. 搭建环境请参考:http://www.cnblogs.com/momo8238/p/7508677.html 2. 创建表结构 models.py from django.db import m ...
- JS 中对变量类型判断的几种方式
文章整理搬运,出处不详,如有侵犯,请联系~ 数据类型判断和数据类型转换代码工具 在 JS 中,有 5 种基本数据类型和 1 种复杂数据类型,基本数据类型有:Undefined, Null, Boo ...
- Activity启动过程中获取组件宽高的五种方式
第一种:(重写Activity的onWindowFocusChanged方法) /** * 重写Acitivty的onWindowFocusChanged方法 */ @Override public ...
- web项目中实现页面跳转的两种方式
<a href="javascript:"></a>跳转在网页本身,URL不改变 <a href="#"></a> ...
- JS中获取页面单选框radio和复选框checkbox中当前选中的值
单选框:单选框的name值全部相同 页面有一组单选框的元素<td><input type="radio name="radioid">满意< ...
随机推荐
- 转:XML 中的空白字符须知:xml:space
了解 XML 空白字符的概念并掌握如何避免与之相关的问题的技巧. 2006 年 4 月发布 很多时候,您可能都没注意到,在 XML 中所做的更改影响着您访问 XML 文档中数据的方式.例如: < ...
- 在mysql中,如何改变列声明.
C 在mysql中,如何改变列声明. 修改表 - 修改列名 使用 CHANGE COLUMN 来修改列的名字,还必须 设置 列的数据类型 mysql> desc test_tab -> / ...
- react中findDOMNode
在使用react过程中,大家有时会那么这里的findDomNode是做什么的呢? import { findDomNode } from 'react-dom'; 简单来说是用来得到实际Dom的,因为 ...
- 各开源 bbs 程序比较
主要是集中在 php 开源轻巧的程序. 搜索到一个逼乎的一个帖子:https://www.zhihu.com/question/20655704 ,顺藤摸瓜 下. carbon forum 第一个测试 ...
- [UIApplication sharedApplication].idleTimerDisabled=YES;不自动锁屏 [UIApplication sharedApplication].idleTimerDisabled=NO;自动锁屏
[UIApplication sharedApplication].idleTimerDisabled=YES;不自动锁屏 [UIApplication sharedApplication].idle ...
- java定时器无法自动注入的问题解析(原来Spring定时器可以这样注入service)
近些日子在做一个项目,在项目中需要用到spring的定时任务,但是在操作数据库的时候发现总是不能正确的进行数据的插入,经过查看才发现:是因为service层不能够通过普通的方法注入到action,所以 ...
- 最新解决 Ubuntu16.04 和 win10 双系统时间同步问题 (设置为 UTC 时间)
最近在电脑上安装了 Ubuntu16.04 和 Win10 双系统, 开机后发现电脑的开机系统出现了问题,不知道怎么搞的总是会出现8个小时的误差,在网上查了好多文章发现网上的大部分方法都是比较过时的 ...
- Buildroot MariaDB替代MySQL
/********************************************************************************* * Buildroot Maria ...
- macOS -- 如何通过终端开启/关闭SSH
在macOS中(较新版),基本都会配置了SSH,能完成我们开发中绝大部分功能,所以不需要再去使用第三方的软件去操作. 不过SSH守护进程是默认禁用的,我们需要手动开启 1. 查看是否开始SSH功能 s ...
- 【转】linux中inittab文件详解
原文网址:http://www.2cto.com/os/201108/98426.html linux中inittab文件详解 init的进程号是1(ps -aux | less),从这一点就能看出, ...