114.简单介绍JavaScript

news/2025/3/31 5:24:42/

首先我们复制我们的起始js代码


```css
///
// Fixing flexbox gap property missing in some Safari versions
function checkFlexGap() {var flex = document.createElement("div");flex.style.display = "flex";flex.style.flexDirection = "column";flex.style.rowGap = "1px";flex.appendChild(document.createElement("div"));flex.appendChild(document.createElement("div"));document.body.appendChild(flex);var isSupported = flex.scrollHeight === 1;flex.parentNode.removeChild(flex);console.log(isSupported);if (!isSupported) document.body.classList.add("no-flexbox-gap");
}
checkFlexGap();// https://unpkg.com/smoothscroll-polyfill@0.4.4/dist/smoothscroll.min.js/*
.no-flexbox-gap .main-nav-list li:not(:last-child) {margin-right: 4.8rem;
}.no-flexbox-gap .list-item:not(:last-child) {margin-bottom: 1.6rem;
}.no-flexbox-gap .list-icon:not(:last-child) {margin-right: 1.6rem;
}.no-flexbox-gap .delivered-faces {margin-right: 1.6rem;
}.no-flexbox-gap .meal-attribute:not(:last-child) {margin-bottom: 2rem;
}.no-flexbox-gap .meal-icon {margin-right: 1.6rem;
}.no-flexbox-gap .footer-row div:not(:last-child) {margin-right: 6.4rem;
}.no-flexbox-gap .social-links li:not(:last-child) {margin-right: 2.4rem;
}.no-flexbox-gap .footer-nav li:not(:last-child) {margin-bottom: 2.4rem;
}@media (max-width: 75em) {.no-flexbox-gap .main-nav-list li:not(:last-child) {margin-right: 3.2rem;}
}@media (max-width: 59em) {.no-flexbox-gap .main-nav-list li:not(:last-child) {margin-right: 0;margin-bottom: 4.8rem;}
}
*/

● 之后在HTML中链接JS文件

<script defer src="js/script.js"></script>

● 接着我们在JavaScript文件的头部添加代码

console.log("Hello world");

在这里插入图片描述

这样证明链接成功了!

定义变量

const myName = '周杰伦';
console.log(myName);

在这里插入图片描述

● 当然我们也可以创建变量去选择HTML文档中的元素

const myName = '周杰伦';
const h1 = document.querySelector('.heading-primary');  ##选择文档中第一个类名为“heading-primary”的元素,并将其存储在名为“h1”的变量中。这个变量可以在后续的代码中被使用
console.log(myName);
console.log(h1);
##在JavaScript中,document 是一个全局对象,代表整个HTML文档。querySelector 是document对象上的一个方法,它可以通过指定CSS选择器来查找文档中的元素。

在这里插入图片描述

● 当然我们也可以给变量赋值

h1.textContent = myName;
##这行代码的意思是将一个名为 myName 的变量的值赋给 h1 元素的文本内容。

在这里插入图片描述

● 除此之外,我们还可以对CSS进行操作

h1.style.backgroundColor = "red";##h1.style.backgroundColor = "red";

在这里插入图片描述

注意:在JavaScript中,属性和方法名不能包含破折号(-),而只能使用驼峰式命名法。因此,在JavaScript中,background-color 这个CSS属性需要写成 backgroundColor,使用驼峰式命名法来表示。这是因为JavaScript中的语法规则所决定的。

● 除此之外,其他的CSS样式也是可以的

h1.style.padding = "3.2rem";

在这里插入图片描述

● 除此之外, 还可以做点击事件

h1.addEventListener("click", function () {  #当点击的时候触发function函数,指定函数里面的内容h1.textContent = myName;h1.style.backgroundColor = "red";h1.style.padding = "3.2rem";
});
##addEventListener 是 JavaScript 中用于添加事件监听器的方法。它可以在指定的元素上添加一个事件监听器,监听指定的事件,当事件被触发时,执行指定的回调函数。

在这里插入图片描述
在这里插入图片描述

● 如图,这个页面的2027并不是我们当前的时间,我们可以适用js简单的获取当前的时间
在这里插入图片描述

● 首先给这个年份添加一下class
在这里插入图片描述

const yearEl = document.querySelector (".year");
const currentYear = new Date().getFullYear();
yearEl.textContent = currentYear;## 这段代码的作用是获取 HTMLclass 为 year 的元素,然后将当前年份设置为该元素的文本内容。##首先,通过 document.querySelector(".year") 获取了 HTMLclass 为 year 的元素,然后通过 new Date().getFullYear() 获取当前年份,并将其赋值给 currentYear 变量。最后,将 currentYear 变量的值设置为 yearEl 元素的文本内容,即将当前年份显示在页面中。
文章来源:https://blog.csdn.net/weixin_42952508/article/details/131441220
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.ppmy.cn/news/623340.html

相关文章

linux段错误core dumped,Linux下Segmentation fault(core dumped)简单调试方法

** 什么是Segmentation fault? ** Segmentation fault就是段错误,一般指访问的内存超出了系统给这个程序所设定的内存空间,例如访问了不存在的内存地址、访问了系统保护的内存地址、访问了只读的内存地址等等情况。 什么是core dumped 操作系统把程序当掉时的内存内容 dump …

CoreDNS 健康检查详解

❝ 本文转自 TinyChen 的博客&#xff0c;原文&#xff1a;https://tinychen.com/20220728-dns-11-coredns-08-healthcheck/&#xff0c;版权归原作者所有。欢迎投稿&#xff0c;投稿请添加微信好友&#xff1a;cloud-native-yang 本文主要讲解介绍 CoreDNS 内置的两个健康检查…

Ubuntu18.04使用记录

​ 1.换源 mv /etc/apt/sources.list /etc/apt/sourses.list.backup vi /etc/apt/sources.list deb http://mirrors.aliyun.com/ubuntu/ bionic main restricted universe multiverse deb http://mirrors.aliyun.com/ubuntu/ bionic-security main restricted universe multi…

Jenkins构建vue项目找不到相关依赖

接上一篇《sourceTree代码回滚》文章中遗留的问题&#xff1a;代码在Jenkins构建的时候发现找不到两个依赖 虽然依赖在本地安装成功&#xff0c;并且记录到了package.json中&#xff0c;但是在代码构建的时候却出现上图报错&#xff0c;尽管按指示重新执行了npm install --save…

16S多样性组成谱研究,9.13分的Water Research轻松二连发!

期刊&#xff1a;Water Research 影响因子&#xff1a;9.13 Water Research是国际水协会IWA出版的水资源研究领域权威期刊&#xff0c;目前影响因子达到9.13&#xff0c;且逐年增长&#xff0c;在水资源同类期刊中持续保持第一。 Water Research影响因子逐年增长&#xff0c;…

学计算机男生喜欢的礼物,十大00后男生喜欢的礼物大集合

每个时代都有它流行的东西&#xff0c;自然不同时代的人喜欢的东西也不一样。00后的男生作为二十一世纪的第一批年轻男士&#xff0c;自然也有他们喜欢的东西。在这主要为大家盘点一些00后男生喜欢的礼物&#xff0c;每一件都充满创意和新奇的元素&#xff0c;送给00后男生不会…

乐高无限怎么换服务器,乐高无限新开服应该怎么玩 玩法攻略技巧

大话白蛇无限版1.0.0最新版 类型&#xff1a;角色扮演大小&#xff1a;105.8M语言&#xff1a;中文 评分&#xff1a;10.0 更新&#xff1a;2019-07-23 立即下载 在今天乐高无限开启了全平台测试&#xff0c;很多小伙伴在开测前都玩过内测版&#xff0c;但是新版本更新之后又有…

2022电工(初级)考试题模拟考试题库及模拟考试

题库来源&#xff1a;安全生产模拟考试一点通公众号小程序 2022年电工&#xff08;初级&#xff09;上岗证题目是电工&#xff08;初级&#xff09;考试题目理论知识考试题库&#xff01;2022电工&#xff08;初级&#xff09;考试题模拟考试题库及模拟考试根据电工&#xff0…