setAttribute()与getAttribute()

2024-03-19 01:32
文章标签 getattribute setattribute

本文主要是介绍setAttribute()与getAttribute(),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

在《JavaScript Dom 编程艺术》中定义了两个函数,getAttribute()与setAttribute(),下面我还是通过前面讲的getElementById等中的一个实例来加以说明,在此我并没有将它的理论定义,具体看实例:

<!DOCTYPE html>
<html>
<head><title>shopping list</title><meta http-equiv="Content-Type" content="text/html; charset=utf-8" /><link href="css/css.css" rel="stylesheet" type="text/css">
</head><h1>what to buy</h1><P title="a gentle reminder">Don't forget to buy this stuff.</P><ul id="purchases"><li name="t" class="sale">A tin of beans</li><li name="t" class="sale">Cheese</li><li name="t" class="sale important">milk</li></ul><div id="div1">hangzhou</div><input type="button" value="按钮" onclick="test()"/><script>function test(){var paras=document.getElementsByTagName("P");for(var i=0;i<paras.length;i++){var x=paras[i].getAttribute("title");if(x!=null){alert(x);}}}function test(){var paras=document.getElementsByTagName("p");for(var i=0;i<paras.length;i++){var x=paras[i].getAttribute("title");if(x){paras[i].setAttribute("title","HelloWorld");var y=paras[i].getAttribute("title");alert(y);}}}</script>
</body>
</html>

注意这两个测试函数test(),要分别测试,我在此只是将他们写在了一起,并没有注释其中的一个。

输出结果分别为:

这里写图片描述

这里写图片描述

这篇关于setAttribute()与getAttribute()的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/824369

相关文章

详解Python中 __get__和__getattr__和__getattribute__的区别

和__getattribu详解Python中 __get__和__getattr__te__的区别 http://www.jb51.net/article/86749.htm 引子 假设我们有个类A,其中a是A的实例 a.x时发生了什么?属性的lookup顺序如下: 如果重载了__getattribute__,则调用.a.__dict__, 实例中是不允许有descriptor的,所以

ESAPI.setAttribute设置值前端取不到

我在后端使用java设置email request.setAttribute("email",ESAPI.encoder().encodeForHTML("123456@qq.com")) 前端jsp页面获取不到, var email="<%=ESAPI.encoder().encodeForHTML(request.getParameter("email"))%>"; 后端设置使用的

python class __getattr__ 与 __getattribute__ 的区别

在Python中,__getattr__是一个特殊的方法,用于处理访问不存在的属性时的行为。它通常在类中被重写,以便在属性访问失败时提供自定义的处理逻辑。 __getattr__ 的使用 1. 基本用法 __getattr__方法在访问类实例的某个不存在的属性时自动调用。其签名如下: def __getattr__(self, name):# 自定义处理逻辑pass self:指向实例

Python __getattr__ __getattribute__ __setattr__

__getattr__ 如果某类定义了__getattr__,同时系统在该类对象的实例字典中又找不到待查询的属性,系统就会调用这个方法,这种行为非常适合无结构数据的按需访问。 class A:def __init__(self):self.name = 'Tom'def __getattr__(self,name):setattr(self, name, name)return "ca

getAttribute和.getParameter的区别

一直对于request的东西感觉很抽象,不是很懂,今天用到了就又查了查,复习了一下。 一个request就是一个对象,setAttribute就是在request scope里面添加了一个变量。setAttribute就是在request的map里面put值 request.getAttribute是取的web容器里的值,在具有转发关系的web组件之间共享(servlet和j

js onchange事件,以及setAttribute,getAttribute

onchange为form表单中select标签发生改变时产生的事件,从                            <select style="width:160px" class="select2" name="mix_{$val.sign}" οnchange="gradeChange(this)" >                             <opt

getAttribute()用法

一份文档就是一棵节点树。 ●节点分为不同的类型:元素节点、属性节点和文本节点等。 ●getElementById()方法将返回一个对象,该对象对应着文档里的一个特定的元素节点。 ●getElementsByTagName()方法将返回一个对象数组,它们分别对应着文档里的一个特定的元素节点。 ●这些节点中的每个都是一个对象。 接下来,我们将

【Echarts】Uncaught TypeError: Cannot read property 'getAttribute' of null报错

原帖地址如下:问题原文处 在一次修改页面的html文件后发现页面的js文件全部挂掉了 然后通过查询在大神聚集的stackoverflow找到了答案 原文如下: 这位小伙子表示自己刚刚开始学习js,他想做一个转盘的js实现,但是在第一行报错。 报的错误和我的一样。 下面的第一个大神给出了答案 翻译: 这个错误的发生是因为当方法被调用的时候这个图像的对象还没有加

Error in nextTick: “TypeError: Cannot read properties of undefined (reading ‘getAttribute’)” 问题的解决

Error in nextTick: “TypeError: Cannot read properties of undefined (reading ‘getAttribute’)” 问题的解决 报错 Cannot read properties of undefined 解决方法:加一个判断if (表示dom已存在),再执行this.nextTick() 方法 if (this.$r

setAttribute的用法

request.setAttribute("user",user);//一般是表示将user对象从Action传递到JSP页面。