js DOM操作练习
1.有如下html,如果用js获得被选中的option的text描述(非value)
<select id="select_id">
<option vlue="1">text1</option>
<option vlue="2">text2</option>
<option vlue="3">text3</option>
<option vlue="4">text4</option>
</select>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>无标题文档</title>
</head> <body>
<select id="select_id">
<option value="text1">text1</option>
<option value="text2">text2</option>
<option value="text3">text3</option>
</select>
<script>
var obj=document.getElementById("select_id"); //获取select元素
obj.onchange=function(){
for(var i=0;i<obj.options.length;i++){
if(obj.options[i].selected)
alert(obj.options[i].text); //输出被选项的文本信息
}
}
</script>
</body>
</html>
2.有如下html,如何用js取得month的值
<form id="form_id">
<input type="radio" name="month"value=1 />
<input type="radio" name="month"value=2 />
<input type="radio" name="month"value=3 />
<input type="radio" name="month"value=4 />
</form>
3.有如下html,如何通过Dom方法来取得最后一个p的text
<html>
<head>
<title>DOM Example</title>
</head>
<body>
<p>Hello World!</p>
<p>Isn'T this exciting!<p>
<p>You're learning to ues ths DOM!</p>
</body>
</html>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>无标题文档</title>
</head>
<body>
<p>Hello World!</p>
<p>Isn'T this exciting!<p>
<p>You're learning to ues ths DOM!</p>
<script>
var obj=document.getElementsByTagName("p");
var txt=obj[obj.length-1].innerHTML||obj[obj.length-1].textContent; //IE与Firefox浏览器兼容
alert(txt);
</script>
</body>
</html>
4..要用标准dom方法来动态在页面body中加入如下html,该如何做?
<p id="pl">hello world</p>
<script>
var p=document.createElement("p"); //创建元素节点
var txt=document.createTextNode("hello world!"); //创建文本节点
p.appendChild(txt); // 将文本节点添加到新创建元素中
document.body.appendChild(p); //将添加了文本的元素添加到页面中
</script>
js DOM操作练习的更多相关文章
- JS DOM操作(创建、遍历、获取、操作、删除节点)
创建节点 <!DOCTYPE html> <html lang="zh-CN"> <head> <meta charset="u ...
- js dom 操作
JS的DOM操作 1DOM是文档对象模型,这种模型为树模型:文档是指标签文档,对象是指文档中每个元素:模型是指抽象化的东西. 2间隔与延迟间隔执行一段代码(函数):window.setInterv ...
- JS—DOM操作
节点分为三类: 1.元素节点:标签<div></div> 2.文本节点:标签内的纯文本. 3.属性节点:标签内的属性,id或class 查找元素: getElementById ...
- js——DOM操作(一)
DOM:Document Object Model 文档对象模型 文档:html页面 文档对象:页面中元素 文档对象模型:定义 为了能够让程序(js)去操作页面中的元素 DOM会把文档看作是一棵树 ...
- JS DOM操作 函数 事件 阻止事件冒泡
一 函数 1.字符串函数 s.tolowerCase( ): -- 变小写 s.toupperCase( ): -- 变大写 s.substr( 2 , 8 ): -- 截取 ...
- JS DOM操作(五) Window.docunment对象——操作元素
定位: var a = document.getElementByIt( "id" ) 同辈元素 var b = a.nextSibling; -- 找 a ...
- JS DOM操作(二) Window.docunment对象——操作样式
一 对元素的定位 在 js 中可以利用 id.class.name.标签名进行元素的定位 id.class 用在客户端 name 用在服务端 用 id 定位 -- ...
- JS DOM操作(一) 对页面的操作
DOM ——文档对象模型(Document Object Model)是表示和处理一个HTML或XML文档的常用方法. 在网页上,组织页面(或文档)的对象被组织在一个树形结构中,用来表示文档中对象的标 ...
- js dom 操作技巧
1.创建元素 创建元素:document.createElement() 使用document.createElement()可以创建新元素.这个方法只接受一个参数,即要创建元素的标签名.这个标签名在 ...
随机推荐
- python 通过句柄获取窗口内容
-- enoding:utf-8 -- 生成 buffer 对象 import win32con from win32gui import PyMakeBuffer, SendMessage, PyG ...
- 解决idea创建ssm项目找不到mybatis的mapper的xml文件问题
http://blog.csdn.net/v19freedom/article/details/69855302 后来上网搜了下,别人给出的答复 idea在build工程的时候 遇到maven项目 使 ...
- 《C语言程序设计(第四版)》阅读心得(三)
第八章 指针 1.一个变量的地址称为该变量的指针 一个专门用来存放另一变量的地址(即指针),称它为指针变量.指针变量的值是指针(即地址) 如上图2000是变量的指针,pointer是指针变量, 赋值 ...
- topcoder SRM 639 div2
见这里 http://ygdtc.sinaapp.com/?p=257
- 快速幂取模模板 && 51nod 1013 3的幂的和
#include <iostream> #include <cstdio> #include <cmath> #include <vector> #in ...
- nyoj_205_求余数_201404271630
求余数 时间限制:1000 ms | 内存限制:65535 KB 难度:3 描述 现在给你一个自然数n,它的位数小于等于一百万,现在你要做的就是求出这个数除10003之后的余数 输入 第一 ...
- MU Puzzle HDU - 4662
Suppose there are the symbols M, I, and U which can be combined to produce strings of symbols called ...
- 2017-10-03-morning
#include <algorithm> #include <cstring> #include <cstdio> inline void read(int &am ...
- 积累——SQLCommand命令
SQLcommand表示要对SQL数据库运行的一个 T-SQL 语句或存储过程.以便运行大量操作或处理数据库结构. 在对数据库訪问的时候,就经经常使用到这个.看看它是怎么做到的吧! 一.属性 Comm ...
- 华为AR1220
今天刚刚收到华为AR1220,以为直接就可以用web界面管理,结果开机后才知道web管理界面需要激活.下面简单分享这个过程:*** 用控制台线(一边RJ45,一边9针串)连接Router consol ...