id的唯一属性,不可重复!!

文章强调了HTML中id属性必须是唯一的,不能重复,即使是隐藏元素(如使用display:none)其id仍然存在。示例说明了当尝试控制两个具有相同id的盒子显示隐藏时,由于id不唯一导致的问题,并提示有其他方法可以更有效地管理元素的可见性。重点在于强调id的唯一性及其重要性。
摘要由CSDN通过智能技术生成

id属性是唯一的,不可重复!!!不可重复!!!!不可重复!!!!!

即使,使用display:none,隐藏掉盒子也是不行的!属性依旧存在!

比如,我想控制两个盒子的显示隐藏

 

这样是没有效果的,因为id不唯一!!!

改成以下这样就可以了

当然了,想要控制两个盒子的显示隐藏还有更简便一些的方法,这里主要是想讲  id的唯一性!!

盒子隐藏并不代表id属性就消失了。 

好的,这是一个比较完整的菜单程序,可以对动物园的动物进行管理。代码如下(注释已加): ```python # 定义动物类 class Animal: def __init__(self, id, type, color, sex, price, buyDate): self.id = id self.type = type self.color = color self.sex = sex self.price = price self.buyDate = buyDate self.isDead = False # 定义动物园类 class Zoo: def __init__(self): self.animals = [] # 添加动物信息 def addAnimal(self, id, type, color, sex, price, buyDate): # 判断id是否已存在 for animal in self.animals: if animal.id == id: print("该动物已存在!") return # 创建新动物实例 newAnimal = Animal(id, type, color, sex, price, buyDate) # 添加至动物园 self.animals.append(newAnimal) print("添加成功!") # 注销一条动物信息(死亡或卖掉) def cancelAnimal(self, id): # 判断id是否存在 for animal in self.animals: if animal.id == id: # 修改isDead属性 animal.isDead = True print("注销成功!") return print("该动物不存在!") # 查询全部动物信息 def queryAll(self): print("所有动物信息:") for animal in self.animals: # 判断isDead属性 if not animal.isDead: print("ID:{}, {}, {}, {}, {}购买, {}入园".format(animal.id, animal.type, animal.color, animal.sex, animal.price, animal.buyDate)) # 查询某种颜色的动物信息 def queryByColor(self, color): print("颜色为{}的动物信息:".format(color)) for animal in self.animals: # 判断isDead属性和颜色 if not animal.isDead and animal.color == color: print("ID:{}, {}, {}, {}, {}购买, {}入园".format(animal.id, animal.type, animal.color, animal.sex, animal.price, animal.buyDate)) # 統計某种类型的动物信息 def countByType(self, type): count = 0 for animal in self.animals: # 判断isDead属性和类型 if not animal.isDead and animal.type == type: count += 1 print("类型为{}的动物数量为{}".format(type, count)) # 统计某种类型的动物的价值 def countPriceByType(self, type): price = 0 for animal in self.animals: # 判断isDead属性和类型 if not animal.isDead and animal.type == type: price += animal.price print("类型为{}的动物总价值为{}".format(type, price)) # 修改某个动物的基本信息(输入id) def modifyAnimal(self, id): # 判断id是否存在 for animal in self.animals: if animal.id == id: # 修改属性 animal.color = input("请输入新颜色:") animal.price = int(input("请输入新价格:")) print("修改成功!") return print("该动物不存在!") # 菜单程序 def menu(self): while True: print("动物园管理系统") print("1.添加动物信息") print("2.注销动物信息") print("3.查询全部动物信息") print("4.查询某种颜色的动物信息") print("5.统计某种类型的动物信息") print("6.统计某种类型的动物的价值") print("7.修改某个动物的基本信息") print("8.退出") choice = input("请输入要进行的操作:") if choice == "1": id = int(input("请输入动物ID:")) type = input("请输入动物类型:") color = input("请输入动物颜色:") sex = input("请输入动物性别:") price = int(input("请输入动物价格:")) buyDate = input("请输入动物入园时间:") self.addAnimal(id, type, color, sex, price, buyDate) elif choice == "2": id = int(input("请输入动物ID:")) self.cancelAnimal(id) elif choice == "3": self.queryAll() elif choice == "4": color = input("请输入颜色:") self.queryByColor(color) elif choice == "5": type = input("请输入类型:") self.countByType(type) elif choice == "6": type = input("请输入类型:") self.countPriceByType(type) elif choice == "7": id = int(input("请输入动物ID:")) self.modifyAnimal(id) elif choice == "8": print("谢谢使用,再见!") break else: print("输入有误,请重新输入!") # 测试 zoo = Zoo() zoo.menu() ``` 使用时,可通过调用菜单程序来实现对动物园动物信息的管理。例如,输入1添加动物信息,输入3查询全部动物信息。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值