HTML DOM方法

querySelector() (querySelector())

The Document method querySelector() returns the first element within the document that matches the specified selector, or group of selectors. If no matches are found, null is returned.

Document方法querySelector()返回文档中与指定选择器或选择器组匹配的first元素。 如果未找到匹配项,则返回null。

HTML内容: (HTML content:)

<div id="id-example"></div>
<div class="class-example"></div>
<a>element-example</a>

JavaScript内容: (JavaScript content:)

document.querySelector("#id-example"); // Returns the element with id "id-example"
document.querySelector(".class-example"); // Returns the element with class "class-example"
document.querySelector("a"); // Returns the "a" element

Note querySelector() returns the first matching element, to return all the matches, use the querySelectorAll() method instead.

注意querySelector()返回第一个匹配的元素,要返回所有匹配项,请改用querySelectorAll()方法。

<div id="example">First</div>
<div id="example">Second</div>
document.querySelector("#example"); // Returns only the element containing 'First'

innerHTML (innerHTML )

The innerHTML prop return the HTML content inside a selected element and also let you define a new HTML content.

innerHTML属性返回选定元素内HTML内容,还允许您定义新HTML内容。

获取元素内容 (Get element content)

<div id="demo"><p>Demo</p>
</div>
var element = document.getElementById("demo");
console.log(element.innerHTML) //logs <p>Demo</p>

设置元素内容 (Set element content)

<div id="demo"></div>
var element = document.getElementById("demo");
element.innerHTML = "<div>Demo</div>";

The HTML now will be like

现在HTML就像

<div id="demo"><div>Demo</div>
</div>

安全注意事项 (Security considerations)

The value that’s set to innerHTML should come from trusted sources, since Javascript will put anything inside that element and it will be run as plain HTML.

设置为innerHTML的值应该来自受信任的来源,因为Javascript会将任何内容放入该元素中,并且它将作为纯HTML运行。

Example:

例:

Setting a ”<script>alert();</script>” value will cause the Javascript “alert()” function to be fired:

设置“ <script>alert();</script> ”值将触发Javascript“ alert()”函数:

var element = document.getElementById("demo");element.innerHTML = "<script>alert();</script>";

This type of attack is called Cross Site Scripting, or XSS for short.

这种攻击称为“ 跨站点脚本”或“ XSS” 。

This is one of the most common ways of committing an XSS attack. If you want to learn a little bit more and learn to defend against it, check out this resource.

这是实施XSS攻击的最常见方法之一。 如果您想学习更多并学会防御, 请查看此资源 。

getElementById() (getElementById())

The getElementById() method returns the element that has the id attribute with the specified value. It takes one argument, which is a case-sensitive string of the id for the element you want.

getElementById()方法返回具有具有指定值的id属性的元素。 它带有一个参数,它是所需元素的ID的区分大小写的字符串。

This method is one of the most common methods in the HTML DOM, and is used almost every time you want to manipulate, or get info from, an element in your document. Here’s a simple example of the syntax:

此方法是HTML DOM中最常见的方法之一,几乎在您每次要操作或从文档中的元素获取信息时都使用该方法。 这是语法的一个简单示例:

HTML content:

HTML内容:

<div id="demo"></div>

JavaScript content:

JavaScript内容:

document.getElementById("demo"); // Returns the element with id "demo"

If you have more than one element with the same value of id (bad practice!), getElementById will return the first element found:

如果您有多个具有相同id值的元素(不好的做法!), getElementById将返回找到的第一个元素:

<div id="demo">First</div>
<div id="demo">Second</div>
document.getElementById("demo"); // Returns the element with id "demo" containing 'First'

更多信息: (More Information:)

document.getElementById()

document.getElementById()

替代解决方案: (Alternative solutions:)

A commonly-used alternative to document.getElementById is using a jQuery selector which you read about more here.

常用的document.getElementById替代方法是使用jQuery选择器,您可以在这里内容。

有关HTML DOM的更多信息 (More info about the HTML DOM)

With the HTML DOM, JavaScript can access and change all the elements of an HTML document.

使用HTML DOM,JavaScript可以访问和更改HTML文档的所有元素。

When a web page is loaded, the browser creates a Document Object Model of the page.

当网页加载时,浏览器会创建一个d ocumentØbject 中号奥德尔页面。

The HTML DOM model is constructed as a tree of Objects:

HTML DOM模型被构造为对象树:

Each element in the DOM is also called a node.

DOM中的每个元素也称为节点。

<html>
<head><title> My title </title>
</head>
<body><a href="#">My Link</a><h1> My header </h1>
</body>
</html>

The DOM for the above HTML is as follows:

以上HTML的DOM如下:

With the object model, JavaScript gets all the power it needs to create dynamic HTML:

通过对象模型,JavaScript获得了创建动态HTML所需的全部功能:

  • JavaScript can change all the HTML elements in the page

    JavaScript可以更改页面中的所有HTML元素
  • JavaScript can change all the HTML attributes in the page

    JavaScript可以更改页面中的所有HTML属性
  • JavaScript can change all the CSS styles in the page

    JavaScript可以更改页面中的所有CSS样式
  • JavaScript can remove existing HTML elements and attributes

    JavaScript可以删除现有HTML元素和属性
  • JavaScript can add new HTML elements and attributes

    JavaScript可以添加新HTML元素和属性
  • JavaScript can react to all existing HTML events in the page

    JavaScript可以对页面中所有现有HTML事件做出React
  • JavaScript can create new HTML events in the page

    JavaScript可以在页面中创建新HTML事件

翻译自: https://www.freecodecamp.org/news/html-dom-methods/

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/390587.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

leetcode 773. 滑动谜题

题目 在一个 2 x 3 的板上&#xff08;board&#xff09;有 5 块砖瓦&#xff0c;用数字 1~5 来表示, 以及一块空缺用 0 来表示. 一次移动定义为选择 0 与一个相邻的数字&#xff08;上下左右&#xff09;进行交换. 最终当板 board 的结果是 [[1,2,3],[4,5,0]] 谜板被解开。…

数据科学领域有哪些技术_领域知识在数据科学中到底有多重要?

数据科学领域有哪些技术Jeremie Harris: “In a way, it’s almost like a data scientist or a data analyst has to be like a private investigator more than just a technical person.”杰里米哈里斯(Jeremie Harris) &#xff1a;“ 从某种意义上说&#xff0c;这就像是数…

python 算术运算

1. 算术运算符与优先级 # -*- coding:utf-8 -*-# 运算符含有,-,*,/,**,//,% # ** 表示^ , 也就是次方 a 2 ** 4 print 2 ** 4 , aa 16 / 5 print 16 / 5 , aa 16.0 / 5 print 16.0 / 5 , a# 结果再进行一次floor a 16.0 // 5.0 print 16.0 // 5.0 , aa 16 // 5 print …

c语言编程时碰到取整去不了_碰到编程墙时如何解开

c语言编程时碰到取整去不了Getting stuck is part of being a programmer, no matter the level. The so-called “easy” problem is actually pretty hard. You’re not exactly sure how to move forward. What you thought would work doesn’t.无论身在何处&#xff0c;陷…

初创公司怎么做销售数据分析_为什么您的初创企业需要数据科学来解决这一危机...

初创公司怎么做销售数据分析The spread of coronavirus is delivering a massive blow to the global economy. The lockdown and work from home restrictions have forced thousands of startups to halt expansion plans, cancel services, and announce layoffs.冠状病毒的…

leetcode 909. 蛇梯棋

题目 N x N 的棋盘 board 上&#xff0c;按从 1 到 N*N 的数字给方格编号&#xff0c;编号 从左下角开始&#xff0c;每一行交替方向。 例如&#xff0c;一块 6 x 6 大小的棋盘&#xff0c;编号如下&#xff1a; r 行 c 列的棋盘&#xff0c;按前述方法编号&#xff0c;棋盘格…

Python基础之window常见操作

一、window的常见操作&#xff1a; cd c:\ #进入C盘d: #从C盘切换到D盘 cd python #进入目录cd .. #往上走一层目录dir #查看目录文件列表cd ../.. #往上上走一层目录 二、常见的文件后缀名&#xff1a; .txt 记事本文本文件.doc word文件.xls excel文件.ppt PPT文件.exe 可执行…

WPF效果(GIS三维篇)

二维的GIS已经被我玩烂了&#xff0c;紧接着就是三维了&#xff0c;哈哈&#xff01;先来看看最简单的效果&#xff1a; 转载于:https://www.cnblogs.com/OhMonkey/p/8954626.html

css注释_CSS注释示例–如何注释CSS

css注释Comments are used in CSS to explain a block of code or to make temporary changes during development. The commented code doesn’t execute.CSS中使用注释来解释代码块或在开发过程中进行临时更改。 注释的代码不执行。 Both single and multi-line comments in…

r软件时间序列分析论文_高度比较的时间序列分析-一篇论文评论

r软件时间序列分析论文数据科学 &#xff0c; 机器学习 (Data Science, Machine Learning) In machine learning with time series, using features extracted from series is more powerful than simply treating a time series in a tabular form, with each date/timestamp …

leetcode 168. Excel表列名称

题目 给你一个整数 columnNumber &#xff0c;返回它在 Excel 表中相对应的列名称。 例如&#xff1a; A -> 1 B -> 2 C -> 3 … Z -> 26 AA -> 27 AB -> 28 … 示例 1&#xff1a; 输入&#xff1a;columnNumber 1 输出&#xff1a;“A” 示例 2&…

飞机订票系统

1 #include <stdio.h>2 #include <stdlib.h>3 #include <string.h>4 #include <conio.h>5 typedef struct flightnode{6 char flight_num[10]; //航班号7 char start_time[10]; //起飞时间8 char end_time[10]; //抵达时间9 char st…

解决Mac10.13 Pod报错 -bash: /usr/local/bin/pod: /System/Library/Frameworks/Ruby.fram

升级10.13以后Pod命令失效&#xff0c;解决办法如下&#xff1a; 终端执行 brew link --overwrite cocoapods 复制代码尝试 Pod 命令是否已经恢复 若报错继续执行 brew reinstall cocoapodsbrew install rubybrew link --overwrite cocoapods 复制代码尝试 Pod 命令是否已经恢复…

angular示例_用示例解释Angular动画

angular示例为什么要使用动画&#xff1f; (Why use Animations?) Modern web components frequently use animations. Cascading Style-sheets (CSS) arms developers with the tools to create impressive animations. Property transitions, uniquely named animations, mu…

selenium抓取_使用Selenium的网络抓取电子商务网站

selenium抓取In this article we will go through a web scraping process of an E-Commerce website. I have designed this particular post to be beginner friendly. So, if you have no prior knowledge about web scraping or Selenium you can still follow along.在本文…

剑指 Offer 37. 序列化二叉树

题目 序列化是将一个数据结构或者对象转换为连续的比特位的操作&#xff0c;进而可以将转换后的数据存储在一个文件或者内存中&#xff0c;同时也可以通过网络传输到另一个计算机环境&#xff0c;采取相反方式重构得到原数据。 请设计一个算法来实现二叉树的序列化与反序列化…

ie8 ajaxSubmit 上传文件提示下载

转载 解决ie下ajaxsubmit上传文件提示下载文件问题 主要是应为放回类型为json&#xff0c;返回text/html转载于:https://www.cnblogs.com/yang-C-J/p/8963278.html

一个简单的 js 时间对象创建

JS中获取时间很常见&#xff0c;凑凑热闹&#xff0c;也获取一个时间对象试试 首先&#xff0c;先了解js的获取时间函数如下&#xff1a; var myDate new Date(); //创建一个时间对象 myDate.getYear(); // 获取当前年份&#xff08;2位&#x…

裁判打分_内在的裁判偏见

裁判打分News flash: being an umpire is hard. Their job is to judge whether a ball that’s capable of moving upwards of 100 MPH or breaking 25 inches crossed through an imaginary zone before being caught. I don’t think many would argue that they have it ea…

数据库sql课程设计_SQL和数据库-初学者完整课程

数据库sql课程设计In this course, Mike Dane will teach you database management basics and SQL.在本课程中&#xff0c;Mike Dane将教您数据库管理基础知识和SQL。 The course starts off with Mike helping you install MySQL on Windows or Mac. Then he explores topic…