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.pswp.cn/news/390587.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/390587.shtml
英文地址,請注明出處:http://en.pswp.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…