javascript 获取和设置属性getAttribute setAttribute
2016-12-25 15:18
399 查看
getAttribute只有一个参数,所查询属性的名字
使用方法:object.getAttribute(attribute);
var pas=document.getElementsByTagName("p");
for(var i=0;i<pas.length;i++){
var titleText=pas[i].getAttribute("title");
if(titleText != null)
{
alert(titleText);}}
//测试结果为:
setAttibute 用法
<script type="text/javascript">
var pas=document.getElementsByTagName("p");
for(var i=0;i<pas.length;i++){
var titleText=pas[i].getAttribute("title");
if(titleText != null)
{
pas[i].setAttribute("title","a new title");
alert(pas[i].getAttribute("title"));
}
}
源代码:
<!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=utf-8" />
<title>无标题文档</title>
</head>
<body>
<p title="this is a story"> 依兰爱情故事</p>
<p > this is only a story</p>
<ol id="feel"></ol>
<li>感人</li>
<li>内涵</li>
<script type="text/javascript">
var pas=document.getElementsByTagName("p");
for(var i=0;i<pas.length;i++){
var titleText=pas[i].getAttribute("title");
if(titleText != null)
{
alert(titleText);
pas[i].setAttribute("title","a new title");
alert(pas[i].getAttribute("title"));
}
}
</script>
</body>
</html>
使用方法:object.getAttribute(attribute);
var pas=document.getElementsByTagName("p");
for(var i=0;i<pas.length;i++){
var titleText=pas[i].getAttribute("title");
if(titleText != null)
{
alert(titleText);}}
//测试结果为:
setAttibute 用法
<script type="text/javascript">
var pas=document.getElementsByTagName("p");
for(var i=0;i<pas.length;i++){
var titleText=pas[i].getAttribute("title");
if(titleText != null)
{
pas[i].setAttribute("title","a new title");
alert(pas[i].getAttribute("title"));
}
}
源代码:
<!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=utf-8" />
<title>无标题文档</title>
</head>
<body>
<p title="this is a story"> 依兰爱情故事</p>
<p > this is only a story</p>
<ol id="feel"></ol>
<li>感人</li>
<li>内涵</li>
<script type="text/javascript">
var pas=document.getElementsByTagName("p");
for(var i=0;i<pas.length;i++){
var titleText=pas[i].getAttribute("title");
if(titleText != null)
{
alert(titleText);
pas[i].setAttribute("title","a new title");
alert(pas[i].getAttribute("title"));
}
}
</script>
</body>
</html>
相关文章推荐
- TDirectory.GetAttributes、TDirectory.SetAttributes获取和设置文件夹属性
- 【封装】java中为什么要将属性进行私有化,又使用set和get方法来设置和获取属性呢?
- PHP中的__get()和__set()方法获取设置私有属性
- PHP中的__get()和__set()方法获取设置私有属性
- WinAPI: GetLocalTime、SetLocalTime、SetSystemTime - 获取与设置系统时间
- Solidworks对图纸属性的获得与设置Get and Set Sheet Properties
- __set($key,$values) 和__get($varName) 魔术方法设置读取私有属性
- 用javascript getComputedStyle获取和设置style的原理
- target.getAttribute获取调用这个程序的元素的属性或id等
- javascript动态生成表格内容并设置属性,获取选中行的值
- TDirectory.GetCreationTime、TDirectory.SetCreationTime获取和设置文件夹创建时间
- Yii2设置别名函数(setAlias)和获取别名函数(getAlias)
- Get Custom Attribute on frontend list page and cart page(列表页获取自定义属性的三种方法)
- JavaScript(19)jQuery HTML 获取和设置内容和属性
- WinAPI: GetDoubleClickTime、SetDoubleClickTime - 获取与设置鼠标双击间隔时间
- js nextSibling属性和previousSibling属性 javascript之getAttribute()方法 应用续
- android properties分析(1) Properties的设置和获取接口 getprop setprop SystemProperties.get SystemProperties.set
- 获取和设置套接字选项——getsockopt/setsockopt
- JavaScript设置获取和设置属性的方法
- 获取listview中的子元素view, 设置textview的图标:setCompoundDrawables使用, view.textView.getHeight()为0