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">满意< ...
随机推荐
- [转载]字符串匹配的KMP算法
作者: 阮一峰 日期: 2013年5月 1日 字符串匹配是计算机的基本任务之一. 举例来说,有一个字符串"BBC ABCDAB ABCDABCDABDE",我想知道,里面是否包含另 ...
- 创芯Xilinx Microblaze 学习系列第一集
创芯Xilinx Microblaze 学习系列第一集 Xilinx ISE Design Suite 13.2 The MicroBlaze™ embedded processor soft cor ...
- SharePoint开发 - TimerJob简单实例讲解
博客地址 http://blog.csdn.net/foxdave SharePoint中的TimerJob类似于Windows系统的计划任务,可以实现定时执行指定操作的功能. 本篇所述的实例为在Sh ...
- scikit-learn 学习笔记-- Generalized Linear Models (一)
scikit-learn 是非常优秀的一个有关机器学习的 Python Lib,包含了除深度学习之外的传统机器学习的绝大多数算法,对于了解传统机器学习是一个很不错的平台.每个算法都有相应的例子,既可以 ...
- ieee80211 phy1: Failed to select rate control algorithm
/************************************************************************ * ieee80211 phy1: Failed t ...
- 从HDU2588:GCD 到 HDU5514:Frogs (欧拉公式)
The greatest common divisor GCD(a,b) of two positive integers a and b,sometimes written (a,b),is the ...
- Codeforces 133A:HQ9+
A. HQ9+ time limit per test 2 seconds memory limit per test 256 megabytes input standard input outpu ...
- 51Nod 1049:最大子段和(dp)
1049 最大子段和 基准时间限制:1 秒 空间限制:131072 KB 分值: 0 难度:基础题 收藏 关注 N个整数组成的序列a[1],a[2],a[3],-,a[n],求该序列如a[i]+ ...
- Ext.js高级组件
第二章:Ext.js高级组件 grid组件 普通方式 表格面板类Ext.grid.Panel xtype(别名):gridpanel.grid title标题.renderTo渲染至.width宽.h ...
- (转)关于fflush(stdin)清空输入缓存流(C/C++)
来源:http://my.oschina.net/deanzhao/blog/79790 1. 为什么 fflush(stdin) 是错的?首先请看以下程序: #include <stdio.h ...