当前位置: 首页 > news >正文

JS操作元素的内容

对象.innerText 属性

对象.innerHTML 属性

<body><div class='box'>文字</div><script>//首先获取元素const box = document.querySelector('.box')console.log(box.innerText)</script>
</body>

1.元素innerText属性

将文本内容添加到标签任意位置

显示为纯文本,不解析标签

2.innetHTML解析标签

<body><div class="box"></div><script>const box = document.querySelector('.box')box.innerHTML =`<u>小狗</u>`</script>
</body>

<!DOCTYPE html>
<html lang="en"><head><meta charset="UTF-8" /><meta http-equiv="X-UA-Compatible" content="IE=edge" /><meta name="viewport" content="width=device-width, initial-scale=1.0" /><title>年会抽奖</title><style>.wrapper {width: 840px;height: 420px;background-color: pink;padding: 100px 250px;box-sizing: border-box;}</style>
</head><body><div class="wrapper"><strong>抽奖</strong><h1>一等奖:<span id="one">???</span></h1><h3>二等奖:<span id="two">???</span></h3><h5>三等奖:<span id="three">???</span></h5></div><script>const arr = ['苹果', '香蕉', '橘子', '荔枝', '梨子', '小狗哦']const random = Math.floor(Math.random() * arr.length)console.log(arr[random])const one = document.querySelector('#one')one.innerHTML = arr[random]arr.splice(random, 1)const random2 = Math.floor(Math.random() * arr.length)const two = document.querySelector('#two')two.innerHTML = arr[random2]arr.splice(random2, 1)const random3 = Math.floor(Math.random() * arr.length)const three = document.querySelector('#three')three.innerHTML = arr[random3]</script>
</body></html>

抽奖案例,我尝试共用一个const random 这样会出现undefined情况。

这样分开写就不会出现冲突情况

操作元素常用属性:

       对象.属性=值

<body><img src="./images/1.webp" alt=""><script>const a = document.querySelector('img')a.src = './images/2.webp'</script>
</body>

能够通过src实现换图操作

练习1:

<body><img src="./images/1.webp" alt=""><script>function getrandom(n, m) {return Math.floor(Math.random() * (m - n + 1)) + n}const img = document.querySelector('img')const random = getrandom(1, 6)img.src = `./images/${random}.webp`</script>
</body>

操作元素样式属性

    <style>div {width: 200px;height: 200px;background-color: pink;}</style>
</head><body><div class="box"></div><script>const box = document.querySelector('.box')box.style.width = '300px'//单词可以直接用,组合单词采用小驼峰命名box.style.backgroundColor = 'hotpink'</script>
</body>

案例:

document.body.style

    <style>body {background: url(./images/desktop_1.jpg) no-repeat top center/cover;}</style><body><script>function getrandom(n, m) {return Math.floor(Math.random() * m - n + 1) + n;}const a = getrandom(1, 10);console.log(a)document.body.style.background = `url(./images/desktop_${a}.jpg)`;</script>
</body>

body可以直接使用不需要获取。上述代码可实现刷新换背景图效果

2.操作元素样式属性(ClassName)

<!DOCTYPE html>
<html lang="en">
<head><meta charset="UTF-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport" content="width=device-width, initial-scale=1.0"><title>Document</title><style>.nav{color: brown;}.box{width: 200px;height: 200px;background-color: aqua;}</style>
</head>
<body><div class="nav">是小狗哦</div><script>const a=document.querySelector('div')a.className='nav box'</script>
</body>
</html>

在这里如果使用ClassName方法需要把之前的加上,否则后面会覆盖前面的。

3.通过classList追加模式(新增)

相关文章:

  • nodejs+vue高校社团管理小程序的设计与实现python-flask-django-php
  • 主干网络篇 | YOLOv8改进之在主干网络中引入密集连接卷积网络DenseNet
  • 政安晨:【深度学习实践】【使用 TensorFlow 和 Keras 为结构化数据构建和训练神经网络】(六)—— 二元分类
  • 2024.3.22 QT
  • 学习要不畏难
  • mybatisplus提示:Property ‘mapperLocations‘ was not specified.
  • 手撕算法-接雨水
  • flutter3_douyin:基于flutter3+dart3短视频直播实例|Flutter3.x仿抖音
  • 话题——AI大模型学习
  • Spring Cloud Gateway Server MVC
  • 移植 Zephyr 到 Art-Pi
  • C语言 数组指针 指针数组
  • Python 全栈系列236 rabbit_agent搭建
  • 微服务(基础篇-003-Nacos集群搭建)
  • 黑帽子学Python
  • [原]深入对比数据科学工具箱:Python和R 非结构化数据的结构化
  • ESLint简单操作
  • If…else
  • Java应用性能调优
  • OSS Web直传 (文件图片)
  • spring + angular 实现导出excel
  • Webpack 4x 之路 ( 四 )
  • 记一次和乔布斯合作最难忘的经历
  • 简析gRPC client 连接管理
  • 力扣(LeetCode)965
  • 前端每日实战:70# 视频演示如何用纯 CSS 创作一只徘徊的果冻怪兽
  • 通过获取异步加载JS文件进度实现一个canvas环形loading图
  • 一些基于React、Vue、Node.js、MongoDB技术栈的实践项目
  • ​决定德拉瓦州地区版图的关键历史事件
  • ​力扣解法汇总1802. 有界数组中指定下标处的最大值
  • #《AI中文版》V3 第 1 章 概述
  • (C++)八皇后问题
  • (react踩过的坑)Antd Select(设置了labelInValue)在FormItem中initialValue的问题
  • (ZT) 理解系统底层的概念是多么重要(by趋势科技邹飞)
  • (独孤九剑)--文件系统
  • (附源码)计算机毕业设计ssm-Java网名推荐系统
  • (一)appium-desktop定位元素原理
  • (转)Linux NTP配置详解 (Network Time Protocol)
  • .NET 中各种混淆(Obfuscation)的含义、原理、实际效果和不同级别的差异(使用 SmartAssembly)
  • .NET成年了,然后呢?
  • .Net环境下的缓存技术介绍
  • .NET文档生成工具ADB使用图文教程
  • //解决validator验证插件多个name相同只验证第一的问题
  • /etc/sudoers (root权限管理)
  • @RequestBody与@ModelAttribute
  • @RequestMapping 的作用是什么?
  • [ C++ ] STL_stack(栈)queue(队列)使用及其重要接口模拟实现
  • [Android Pro] Notification的使用
  • [ASP.NET 控件实作 Day7] 设定工具箱的控件图标
  • [C++基础]-初识模板
  • [cocos2d-x]关于CC_CALLBACK
  • [codevs1288] 埃及分数
  • [Hibernate] - Fetching strategies
  • [HNOI2008]玩具装箱toy
  • [IDF]聪明的小羊