```
在 {{domxref("HTMLElement/dragenter_event", "dragenter")}} 和 {{domxref("HTMLElement/dragover_event", "dragover")}} 事件中调用 {{domxref("Event.preventDefault","preventDefault()")}} 方法将表明在该位置允许放置。但是,你通常希望只在某些情况下调用 {{domxref("Event.preventDefault","preventDefault()")}} 方法(如只当拖拽的是链接时)。
@@ -195,14 +198,14 @@ event.dataTransfer.dropEffect = "copy";
```js
function contains(list, value) {
- for( var i = 0; i < list.length; ++i ) {
- if(list[i] === value) return true;
- }
- return false;
+ for (var i = 0; i < list.length; ++i) {
+ if (list[i] === value) return true;
+ }
+ return false;
}
function doDragOver(event) {
- var isLink = contains( event.dataTransfer.types, "text/uri-list");
+ var isLink = contains(event.dataTransfer.types, "text/uri-list");
if (isLink) {
event.preventDefault();
}
diff --git a/files/zh-cn/web/api/html_drag_and_drop_api/file_drag_and_drop/index.md b/files/zh-cn/web/api/html_drag_and_drop_api/file_drag_and_drop/index.md
index 7cfd7d83fc6474..520a3c6bda4ef0 100644
--- a/files/zh-cn/web/api/html_drag_and_drop_api/file_drag_and_drop/index.md
+++ b/files/zh-cn/web/api/html_drag_and_drop_api/file_drag_and_drop/index.md
@@ -24,7 +24,10 @@ HTML 拖放接口使得 web 应用能够在网页中拖放文件。这篇文档
一般来说,在实际应用中需要定义一个 [`dragover`](/zh-CN/docs/Web/API/HTMLElement/dragover_event) 事件的处理函数并在其中加入关闭浏览器默认拖放行为的代码。你需要定义一个 {{domxref("GlobalEventHandlers.ondragover","ondragover")}} 事件处理函数:
```html
-
+
Drag one or more files to this Drop Zone ...
```
@@ -34,7 +37,7 @@ HTML 拖放接口使得 web 应用能够在网页中拖放文件。这篇文档
```css
#drop_zone {
border: 5px solid blue;
- width: 200px;
+ width: 200px;
height: 100px;
}
```
@@ -51,7 +54,7 @@ HTML 拖放接口使得 web 应用能够在网页中拖放文件。这篇文档
```js
function dropHandler(ev) {
- console.log('File(s) dropped');
+ console.log("File(s) dropped");
// Prevent default behavior (Prevent file from being opened)
ev.preventDefault();
@@ -60,15 +63,17 @@ function dropHandler(ev) {
// Use DataTransferItemList interface to access the file(s)
for (var i = 0; i < ev.dataTransfer.items.length; i++) {
// If dropped items aren't files, reject them
- if (ev.dataTransfer.items[i].kind === 'file') {
+ if (ev.dataTransfer.items[i].kind === "file") {
var file = ev.dataTransfer.items[i].getAsFile();
- console.log('... file[' + i + '].name = ' + file.name);
+ console.log("... file[" + i + "].name = " + file.name);
}
}
} else {
// Use DataTransfer interface to access the file(s)
for (var i = 0; i < ev.dataTransfer.files.length; i++) {
- console.log('... file[' + i + '].name = ' + ev.dataTransfer.files[i].name);
+ console.log(
+ "... file[" + i + "].name = " + ev.dataTransfer.files[i].name,
+ );
}
}
}
@@ -80,7 +85,7 @@ function dropHandler(ev) {
```js
function dragOverHandler(ev) {
- console.log('File(s) in drop zone');
+ console.log("File(s) in drop zone");
// Prevent default behavior (Prevent file from being opened)
ev.preventDefault();
diff --git a/files/zh-cn/web/api/html_drag_and_drop_api/index.md b/files/zh-cn/web/api/html_drag_and_drop_api/index.md
index cfabac6930fd68..96177af346c0c6 100644
--- a/files/zh-cn/web/api/html_drag_and_drop_api/index.md
+++ b/files/zh-cn/web/api/html_drag_and_drop_api/index.md
@@ -5,7 +5,7 @@ slug: Web/API/HTML_Drag_and_Drop_API
{{DefaultAPISidebar("HTML Drag and Drop API")}}
-**HTML 拖放**(Drag and Drop)接口使应用程序能够在浏览器中使用拖放功能。例如,用户可使用鼠标选择可拖拽(*draggable*)元素,将元素拖拽到可放置(*droppable*)元素,并释放鼠标按钮以放置这些元素。拖拽操作期间,会有一个可拖拽元素的半透明快照跟随着鼠标指针。
+**HTML 拖放**(Drag and Drop)接口使应用程序能够在浏览器中使用拖放功能。例如,用户可使用鼠标选择可拖拽(_draggable_)元素,将元素拖拽到可放置(_droppable_)元素,并释放鼠标按钮以放置这些元素。拖拽操作期间,会有一个可拖拽元素的半透明快照跟随着鼠标指针。
对于网站、扩展以及 XUL 应用程序,你可以自定义什么元素是可拖拽的、可拖拽元素产生的反馈类型,以及可放置的元素。
@@ -19,15 +19,15 @@ HTML 的 drag & drop 使用了 {{domxref("Event","DOM event model")}} 以及从
所有的 [拖拽事件类型](/zh-CN/docs/Web/API/DragEvent#Event_types) 有一个对应的 [拖拽全局属性](/zh-CN/docs/Web/API/DragEvent#GlobalEventHandlers)。每个拖拽事件类型和拖拽全局属性都有对应的描述文档。下面的表格提供了一个简短的事件类型描述,以及一个相关文档的链接。
-| 事件 | On 型事件处理程序 | 触发时刻 |
-| ---------------------------- | -------------------------------------------------------------------------------- | --------------------------------------------------------------------------------------------------------------------- |
-| [`drag`](/zh-CN/docs/Web/API/HTMLElement/drag_event) | {{domxref('GlobalEventHandlers.ondrag','ondrag')}} | 当拖拽元素或选中的文本时触发。 |
-| [`dragend`](/zh-CN/docs/Web/API/HTMLElement/dragend_event) | {{domxref('GlobalEventHandlers.ondragend','ondragend')}} | 当拖拽操作结束时触发 (比如松开鼠标按键或敲“Esc”键). (见[结束拖拽](/zh-CN/docs/DragDrop/Drag_Operations#dragend)) |
+| 事件 | On 型事件处理程序 | 触发时刻 |
+| -------------------------------------------------------------- | ------------------------------------------------------------ | --------------------------------------------------------------------------------------------------------------------- |
+| [`drag`](/zh-CN/docs/Web/API/HTMLElement/drag_event) | {{domxref('GlobalEventHandlers.ondrag','ondrag')}} | 当拖拽元素或选中的文本时触发。 |
+| [`dragend`](/zh-CN/docs/Web/API/HTMLElement/dragend_event) | {{domxref('GlobalEventHandlers.ondragend','ondragend')}} | 当拖拽操作结束时触发 (比如松开鼠标按键或敲“Esc”键). (见[结束拖拽](/zh-CN/docs/DragDrop/Drag_Operations#dragend)) |
| [`dragenter`](/zh-CN/docs/Web/API/HTMLElement/dragenter_event) | {{domxref('GlobalEventHandlers.ondragenter','ondragenter')}} | 当拖拽元素或选中的文本到一个可释放目标时触发(见 [指定释放目标](/zh-CN/docs/DragDrop/Drag_Operations#droptargets))。 |
| [`dragleave`](/zh-CN/docs/Web/API/HTMLElement/dragleave_event) | {{domxref('GlobalEventHandlers.ondragleave','ondragleave')}} | 当拖拽元素或选中的文本离开一个可释放目标时触发。 |
-| [`dragover`](/zh-CN/docs/Web/API/HTMLElement/dragover_event) | {{domxref('GlobalEventHandlers.ondragover','ondragover')}} | 当元素或选中的文本被拖到一个可释放目标上时触发(每 100 毫秒触发一次)。 |
+| [`dragover`](/zh-CN/docs/Web/API/HTMLElement/dragover_event) | {{domxref('GlobalEventHandlers.ondragover','ondragover')}} | 当元素或选中的文本被拖到一个可释放目标上时触发(每 100 毫秒触发一次)。 |
| [`dragstart`](/zh-CN/docs/Web/API/HTMLElement/dragstart_event) | {{domxref('GlobalEventHandlers.ondragstart','ondragstart')}} | 当用户开始拖拽一个元素或选中的文本时触发(见[开始拖拽操作](/zh-CN/docs/DragDrop/Drag_Operations#dragstart))。 |
-| [`drop`](/zh-CN/docs/Web/API/HTMLElement/drop_event) | {{domxref('GlobalEventHandlers.ondrop','ondrop')}} | 当元素或选中的文本在可释放目标上被释放时触发(见[执行释放](/zh-CN/docs/DragDrop/Drag_Operations#drop))。 |
+| [`drop`](/zh-CN/docs/Web/API/HTMLElement/drop_event) | {{domxref('GlobalEventHandlers.ondrop','ondrop')}} | 当元素或选中的文本在可释放目标上被释放时触发(见[执行释放](/zh-CN/docs/DragDrop/Drag_Operations#drop))。 |
**注意:**当从操作系统向浏览器中拖拽文件时,不会触发 `dragstart` 和`dragend` 事件。
@@ -68,7 +68,7 @@ Mozilla 和 Firefox 支持一些不在标准拖放模型中的特性。请查看
ev.dataTransfer.setData("text/plain", ev.target.id);
}
- window.addEventListener('DOMContentLoaded', () => {
+ window.addEventListener("DOMContentLoaded", () => {
// Get the element by id
const element = document.getElementById("p1");
// Add the ondragstart event listener
@@ -92,7 +92,10 @@ function dragstart_handler(ev) {
// 添加拖拽数据
ev.dataTransfer.setData("text/plain", ev.target.innerText);
ev.dataTransfer.setData("text/html", ev.target.outerHTML);
- ev.dataTransfer.setData("text/uri-list", ev.target.ownerDocument.location.href);
+ ev.dataTransfer.setData(
+ "text/uri-list",
+ ev.target.ownerDocument.location.href,
+ );
}
```
@@ -108,7 +111,7 @@ function dragstart_handler(ev) {
// NOTE: change "example.gif" to a real image URL or the image
// will not be created and the default drag image will be used.
var img = new Image();
- img.src = 'example.gif';
+ img.src = "example.gif";
ev.dataTransfer.setDragImage(img, 10, 10);
}
```
@@ -143,19 +146,24 @@ function dragstart_handler(ev) {
```html
-
Drop Zone
+
+ Drop Zone
+
```
注意每个处理程序调用 {{domxref("Event.preventDefault","preventDefault()")}} 来阻止对这个事件的其他处理过程(如触点事件或指针事件)。
@@ -170,25 +178,32 @@ function drop_handler(ev) {
```html
-
This element is draggable.
-
Drop Zone
+
+ This element is draggable.
+
+
+ Drop Zone
+
```
更多信息请参见 [执行释放](/zh-CN/docs/Web/Guide/HTML/Drag_operations#drop)。
diff --git a/files/zh-cn/web/api/html_drag_and_drop_api/recommended_drag_types/index.md b/files/zh-cn/web/api/html_drag_and_drop_api/recommended_drag_types/index.md
index a4e6c7c0469a78..2bd74f660af3da 100644
--- a/files/zh-cn/web/api/html_drag_and_drop_api/recommended_drag_types/index.md
+++ b/files/zh-cn/web/api/html_drag_and_drop_api/recommended_drag_types/index.md
@@ -119,7 +119,7 @@ Note that the latest spec now dictates that {{domxref("DataTransfer.types")}} sh
As a result, the [contains](/zh-CN/docs/Web/API/Node/contains) method no longer works on the property; the [includes](/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/Array/includes) method should be used instead to check if a specific type of data is provided, using code like the following:
```js
-if ([...event.dataTransfer.types].includes('text/html')) {
+if ([...event.dataTransfer.types].includes("text/html")) {
// Do something
}
```
@@ -167,52 +167,79 @@ There are cases in which you may want to add a file to an existing drag event se
currentEvent.dataTransfer.setData("text/x-moz-url", URL);
currentEvent.dataTransfer.setData("application/x-moz-file-promise-url", URL);
-currentEvent.dataTransfer.setData("application/x-moz-file-promise-dest-filename", leafName);
-currentEvent.dataTransfer.mozSetDataAt('application/x-moz-file-promise',
- new dataProvider(success,error),
- 0, Components.interfaces.nsISupports);
-
-function dataProvider(){}
+currentEvent.dataTransfer.setData(
+ "application/x-moz-file-promise-dest-filename",
+ leafName,
+);
+currentEvent.dataTransfer.mozSetDataAt(
+ "application/x-moz-file-promise",
+ new dataProvider(success, error),
+ 0,
+ Components.interfaces.nsISupports,
+);
+
+function dataProvider() {}
dataProvider.prototype = {
- QueryInterface : function(iid) {
- if (iid.equals(Components.interfaces.nsIFlavorDataProvider)
- || iid.equals(Components.interfaces.nsISupports))
+ QueryInterface: function (iid) {
+ if (
+ iid.equals(Components.interfaces.nsIFlavorDataProvider) ||
+ iid.equals(Components.interfaces.nsISupports)
+ )
return this;
throw Components.results.NS_NOINTERFACE;
},
- getFlavorData : function(aTransferable, aFlavor, aData, aDataLen) {
- if (aFlavor == 'application/x-moz-file-promise') {
-
- var urlPrimitive = {};
- var dataSize = {};
-
- aTransferable.getTransferData('application/x-moz-file-promise-url', urlPrimitive, dataSize);
- var url = urlPrimitive.value.QueryInterface(Components.interfaces.nsISupportsString).data;
- console.log("URL file orignal is = " + url);
-
- var namePrimitive = {};
- aTransferable.getTransferData('application/x-moz-file-promise-dest-filename', namePrimitive, dataSize);
- var name = namePrimitive.value.QueryInterface(Components.interfaces.nsISupportsString).data;
-
- console.log("target filename is = " + name);
-
- var dirPrimitive = {};
- aTransferable.getTransferData('application/x-moz-file-promise-dir', dirPrimitive, dataSize);
- var dir = dirPrimitive.value.QueryInterface(Components.interfaces.nsILocalFile);
-
- console.log("target folder is = " + dir.path);
-
- var file = Cc['@mozilla.org/file/local;1'].createInstance(Components.interfaces.nsILocalFile);
- file.initWithPath(dir.path);
- file.appendRelativePath(name);
-
- console.log("output final path is =" + file.path);
-
- // now you can write or copy the file yourself...
+ getFlavorData: function (aTransferable, aFlavor, aData, aDataLen) {
+ if (aFlavor == "application/x-moz-file-promise") {
+ var urlPrimitive = {};
+ var dataSize = {};
+
+ aTransferable.getTransferData(
+ "application/x-moz-file-promise-url",
+ urlPrimitive,
+ dataSize,
+ );
+ var url = urlPrimitive.value.QueryInterface(
+ Components.interfaces.nsISupportsString,
+ ).data;
+ console.log("URL file orignal is = " + url);
+
+ var namePrimitive = {};
+ aTransferable.getTransferData(
+ "application/x-moz-file-promise-dest-filename",
+ namePrimitive,
+ dataSize,
+ );
+ var name = namePrimitive.value.QueryInterface(
+ Components.interfaces.nsISupportsString,
+ ).data;
+
+ console.log("target filename is = " + name);
+
+ var dirPrimitive = {};
+ aTransferable.getTransferData(
+ "application/x-moz-file-promise-dir",
+ dirPrimitive,
+ dataSize,
+ );
+ var dir = dirPrimitive.value.QueryInterface(
+ Components.interfaces.nsILocalFile,
+ );
+
+ console.log("target folder is = " + dir.path);
+
+ var file = Cc["@mozilla.org/file/local;1"].createInstance(
+ Components.interfaces.nsILocalFile,
+ );
+ file.initWithPath(dir.path);
+ file.appendRelativePath(name);
+
+ console.log("output final path is =" + file.path);
+
+ // now you can write or copy the file yourself...
}
- }
-}
+ },
+};
```
## 参见
diff --git a/files/zh-cn/web/api/htmlaudioelement/audio/index.md b/files/zh-cn/web/api/htmlaudioelement/audio/index.md
index 6f4eb3340d8c54..159eb49871a365 100644
--- a/files/zh-cn/web/api/htmlaudioelement/audio/index.md
+++ b/files/zh-cn/web/api/htmlaudioelement/audio/index.md
@@ -37,7 +37,7 @@ audioObj = new Audio(url);
基于事件的方法是最优的:
```js
-myAudioElement.addEventListener("canplaythrough", event => {
+myAudioElement.addEventListener("canplaythrough", (event) => {
/* 音频可以播放;如果权限允许则播放 */
myAudioElement.play();
});
diff --git a/files/zh-cn/web/api/htmlaudioelement/index.md b/files/zh-cn/web/api/htmlaudioelement/index.md
index d5ecd348a5de86..8562cb3c9edb73 100644
--- a/files/zh-cn/web/api/htmlaudioelement/index.md
+++ b/files/zh-cn/web/api/htmlaudioelement/index.md
@@ -40,7 +40,7 @@ _以下方法是未标准化的,请勿使用._
你可以完全使用 JavaScript 的 {{domxref("HTMLAudioElement.Audio", "Audio()")}} 构造函数来创建一个 `HTMLAudioElement` :
```js
-var audioElement = new Audio('car_horn.wav');
+var audioElement = new Audio("car_horn.wav");
```
然后你可以在这个元素上调用 `play()` 方法
@@ -54,11 +54,11 @@ audioElement.play();
一些经常被使用的属性,包括 {{domxref("HTMLMediaElement.src", "src")}}、{{domxref("HTMLMediaElement.currentTime", "currentTime")}}、{{domxref("HTMLMediaElement.duration", "duration")}}、{{domxref("HTMLMediaElement.paused", "paused")}}、{{domxref("HTMLMediaElement.muted", "muted")}} 和 {{domxref("HTMLMediaElement.volume", "volume")}}。以下这段代码赋值音频文件的播放时长给一个变量:
```js
-var audioElement = new Audio('car_horn.wav');
-audioElement.addEventListener('loadeddata', () => {
+var audioElement = new Audio("car_horn.wav");
+audioElement.addEventListener("loadeddata", () => {
let duration = audioElement.duration;
// duration 变量现在存放音频的播放时长(单位秒)
-})
+});
```
## 事件
diff --git a/files/zh-cn/web/api/htmlbuttonelement/index.md b/files/zh-cn/web/api/htmlbuttonelement/index.md
index 634acd1477735e..76e7d737e2d420 100644
--- a/files/zh-cn/web/api/htmlbuttonelement/index.md
+++ b/files/zh-cn/web/api/htmlbuttonelement/index.md
@@ -166,10 +166,10 @@ _从父对象{{domxref("HTMLElement")}}继承的属性。_
_从父对象{{domxref("HTMLElement")}}继承的方法。_
-| 名字 | 返回类型 | 描述 |
-| --------------------------------------- | ---------------------------- | ------------------ |
+| 名字 | 返回类型 | 描述 |
+| --------------------------------------- | ---------------------- | ------------------ |
| `checkValidity()` | {{domxref("Boolean")}} | 对按钮元素不支持。 |
-| `setCustomValidity(in DOMString error)` | `void` | 对按钮元素不支持。 |
+| `setCustomValidity(in DOMString error)` | `void` | 对按钮元素不支持。 |
在基于 Gecko 的浏览器中,在表单验证时会使用{{cssxref(":-moz-submit-invalid")}} 伪类修饰提交按钮。
diff --git a/files/zh-cn/web/api/htmlcanvaselement/capturestream/index.md b/files/zh-cn/web/api/htmlcanvaselement/capturestream/index.md
index 05b59752a6732c..125381ed1a8528 100644
--- a/files/zh-cn/web/api/htmlcanvaselement/capturestream/index.md
+++ b/files/zh-cn/web/api/htmlcanvaselement/capturestream/index.md
@@ -26,7 +26,7 @@ MediaStream = canvas.captureStream(frameRate);
```js
//获取所需要截取媒体流的 canvas element
-var canvasElt = document.querySelector('canvas');
+var canvasElt = document.querySelector("canvas");
//截取到媒体流
var stream = canvasElt.captureStream(25); // 25 FPS
diff --git a/files/zh-cn/web/api/htmlcanvaselement/getcontext/index.md b/files/zh-cn/web/api/htmlcanvaselement/getcontext/index.md
index 55988ca4aa1cea..116a1f1cf818e7 100644
--- a/files/zh-cn/web/api/htmlcanvaselement/getcontext/index.md
+++ b/files/zh-cn/web/api/htmlcanvaselement/getcontext/index.md
@@ -34,9 +34,7 @@ var ctx = canvas.getContext(contextType, contextAttributes);
- : 你可以在创建渲染上下文的时候设置多个属性,例如:
```js
- canvas.getContext("webgl",
- { antialias: false,
- depth: false });
+ canvas.getContext("webgl", { antialias: false, depth: false });
```
2d 上下文属性:
diff --git a/files/zh-cn/web/api/htmlcanvaselement/height/index.md b/files/zh-cn/web/api/htmlcanvaselement/height/index.md
index 89c5984976a69d..d344337d939327 100644
--- a/files/zh-cn/web/api/htmlcanvaselement/height/index.md
+++ b/files/zh-cn/web/api/htmlcanvaselement/height/index.md
@@ -27,7 +27,7 @@ canvas.height = pxl;
可以通过以下代码获得这个 canvas 元素的高度:
```js
-var canvas = document.getElementById('canvas');
+var canvas = document.getElementById("canvas");
console.log(canvas.height); // 300
```
diff --git a/files/zh-cn/web/api/htmlcanvaselement/mozopaque/index.md b/files/zh-cn/web/api/htmlcanvaselement/mozopaque/index.md
index 3d26511baf9839..b8df10bfe527a1 100644
--- a/files/zh-cn/web/api/htmlcanvaselement/mozopaque/index.md
+++ b/files/zh-cn/web/api/htmlcanvaselement/mozopaque/index.md
@@ -27,7 +27,7 @@ canvas.mozOpaque = true;
你可以获取或设置 `mozOpaque` 属性。例如,当 mimeType == 'image/jpeg'或类似值时,可以将其属性值设置为 true,以在不需要半透明度的情况下提高应用程序的性能。
```js
-var canvas = document.getElementById('canvas');
+var canvas = document.getElementById("canvas");
console.log(canvas.mozOpaque); // true
// 停用该方法
canvas.mozOpaque = false;
diff --git a/files/zh-cn/web/api/htmlcanvaselement/toblob/index.md b/files/zh-cn/web/api/htmlcanvaselement/toblob/index.md
index b3f47df85bba99..fe001e3d515d1f 100644
--- a/files/zh-cn/web/api/htmlcanvaselement/toblob/index.md
+++ b/files/zh-cn/web/api/htmlcanvaselement/toblob/index.md
@@ -14,7 +14,7 @@ slug: Web/API/HTMLCanvasElement/toBlob
## 语法
```js
-toBlob(callback, type, quality)
+toBlob(callback, type, quality);
```
### 参数
@@ -44,11 +44,11 @@ toBlob(callback, type, quality)
```js
var canvas = document.getElementById("canvas");
-canvas.toBlob(function(blob) {
+canvas.toBlob(function (blob) {
var newImg = document.createElement("img"),
- url = URL.createObjectURL(blob);
+ url = URL.createObjectURL(blob);
- newImg.onload = function() {
+ newImg.onload = function () {
// no longer need to read the blob so it's revoked
URL.revokeObjectURL(url);
};
@@ -81,17 +81,20 @@ ctx.fillStyle = "yellow";
ctx.fill();
function blobCallback(iconName) {
- return function(b) {
+ return function (b) {
var a = document.createElement("a");
a.textContent = "Download";
document.body.appendChild(a);
a.style.display = "block";
a.download = iconName + ".ico";
a.href = window.URL.createObjectURL(b);
- }
+ };
}
-canvas.toBlob(blobCallback('passThisString'), 'image/vnd.microsoft.icon',
- '-moz-parse-options:format=bmp;bpp=32');
+canvas.toBlob(
+ blobCallback("passThisString"),
+ "image/vnd.microsoft.icon",
+ "-moz-parse-options:format=bmp;bpp=32",
+);
```
### 使用 OS.File 保存图像到本地(chrome/add-on context only)
@@ -99,42 +102,48 @@ canvas.toBlob(blobCallback('passThisString'), 'image/vnd.microsoft.icon',
> **备注:** 此方法可将 toBlob 生成的图片保存到本地,但仅在 Firefox、Chrome 上下文或带有相关插件的情况下可用,因为 Web 并不存在 OS API。
```js
-const canvas = document.getElementById('canvas');
+const canvas = document.getElementById("canvas");
const d = canvas.width;
-ctx = canvas.getContext('2d');
+ctx = canvas.getContext("2d");
ctx.beginPath();
ctx.moveTo(d / 2, 0);
ctx.lineTo(d, d);
ctx.lineTo(0, d);
ctx.closePath();
-ctx.fillStyle = 'yellow';
+ctx.fillStyle = "yellow";
ctx.fill();
function blobCallback(iconName) {
- return function(b) {
+ return function (b) {
const r = new FileReader();
r.onloadend = function () {
- // r.result contains the ArrayBuffer.
- Cu.import('resource://gre/modules/osfile.jsm');
- const writePath = OS.Path.join(OS.Constants.Path.desktopDir,
- iconName + '.ico');
- const promise = OS.File.writeAtomic(writePath, new Uint8Array(r.result),
- {tmpPath:writePath + '.tmp'});
- promise.then(
- function() {
- console.log('successfully wrote file');
- },
- function() {
- console.log('failure writing file')
- }
- );
+ // r.result contains the ArrayBuffer.
+ Cu.import("resource://gre/modules/osfile.jsm");
+ const writePath = OS.Path.join(
+ OS.Constants.Path.desktopDir,
+ iconName + ".ico",
+ );
+ const promise = OS.File.writeAtomic(writePath, new Uint8Array(r.result), {
+ tmpPath: writePath + ".tmp",
+ });
+ promise.then(
+ function () {
+ console.log("successfully wrote file");
+ },
+ function () {
+ console.log("failure writing file");
+ },
+ );
+ };
+ r.readAsArrayBuffer(b);
};
- r.readAsArrayBuffer(b);
- }
}
-canvas.toBlob(blobCallback('passThisString'), 'image/vnd.microsoft.icon',
- '-moz-parse-options:format=bmp;bpp=32');
+canvas.toBlob(
+ blobCallback("passThisString"),
+ "image/vnd.microsoft.icon",
+ "-moz-parse-options:format=bmp;bpp=32",
+);
```
## 规范
diff --git a/files/zh-cn/web/api/htmlcanvaselement/todataurl/index.md b/files/zh-cn/web/api/htmlcanvaselement/todataurl/index.md
index c25a5c59d9f72a..00c5208a255205 100644
--- a/files/zh-cn/web/api/htmlcanvaselement/todataurl/index.md
+++ b/files/zh-cn/web/api/htmlcanvaselement/todataurl/index.md
@@ -82,10 +82,14 @@ function showGrayImg() {
function removeColors() {
var aImages = document.getElementsByClassName("grayscale"),
- nImgsLen = aImages.length,
- oCanvas = document.createElement("canvas"),
- oCtx = oCanvas.getContext("2d");
- for (var nWidth, nHeight, oImgData, oGrayImg, nPixel, aPix, nPixLen, nImgId = 0; nImgId < nImgsLen; nImgId++) {
+ nImgsLen = aImages.length,
+ oCanvas = document.createElement("canvas"),
+ oCtx = oCanvas.getContext("2d");
+ for (
+ var nWidth, nHeight, oImgData, oGrayImg, nPixel, aPix, nPixLen, nImgId = 0;
+ nImgId < nImgsLen;
+ nImgId++
+ ) {
oColorImg = aImages[nImgId];
nWidth = oColorImg.offsetWidth;
nHeight = oColorImg.offsetHeight;
@@ -96,7 +100,10 @@ function removeColors() {
aPix = oImgData.data;
nPixLen = aPix.length;
for (nPixel = 0; nPixel < nPixLen; nPixel += 4) {
- aPix[nPixel + 2] = aPix[nPixel + 1] = aPix[nPixel] = (aPix[nPixel] + aPix[nPixel + 1] + aPix[nPixel + 2]) / 3;
+ aPix[nPixel + 2] =
+ aPix[nPixel + 1] =
+ aPix[nPixel] =
+ (aPix[nPixel] + aPix[nPixel + 1] + aPix[nPixel + 2]) / 3;
}
oCtx.putImageData(oImgData, 0, 0);
oGrayImg = new Image();
diff --git a/files/zh-cn/web/api/htmlcanvaselement/transfercontroltooffscreen/index.md b/files/zh-cn/web/api/htmlcanvaselement/transfercontroltooffscreen/index.md
index 14dfd556edefff..e815b870483b9d 100644
--- a/files/zh-cn/web/api/htmlcanvaselement/transfercontroltooffscreen/index.md
+++ b/files/zh-cn/web/api/htmlcanvaselement/transfercontroltooffscreen/index.md
@@ -20,9 +20,9 @@ OffscreenCanvas HTMLCanvasElement.transferControlToOffscreen()
## 样例
```js
-var htmlCanvas = document.createElement('canvas');
+var htmlCanvas = document.createElement("canvas");
var offscreen = htmlCanvas.transferControlToOffscreen();
-var gl = offscreen.getContext('webgl');
+var gl = offscreen.getContext("webgl");
// ... some drawing using the gl context ...
diff --git a/files/zh-cn/web/api/htmlcanvaselement/webglcontextlost_event/index.md b/files/zh-cn/web/api/htmlcanvaselement/webglcontextlost_event/index.md
index a8001489334413..680eb209ba19d2 100644
--- a/files/zh-cn/web/api/htmlcanvaselement/webglcontextlost_event/index.md
+++ b/files/zh-cn/web/api/htmlcanvaselement/webglcontextlost_event/index.md
@@ -1,5 +1,5 @@
---
-title: 'HTMLCanvasElement: webglcontextlost event'
+title: "HTMLCanvasElement: webglcontextlost event"
slug: Web/API/HTMLCanvasElement/webglcontextlost_event
---
@@ -33,15 +33,15 @@ slug: Web/API/HTMLCanvasElement/webglcontextlost_event
在 {{domxref("WEBGL_lose_context")}} 扩展的帮助下,您可以模拟 `webglcontextlost` 事件:
```js
-const canvas = document.getElementById('canvas');
-const gl = canvas.getContext('webgl');
+const canvas = document.getElementById("canvas");
+const gl = canvas.getContext("webgl");
-canvas.addEventListener('webglcontextlost', (event) => {
+canvas.addEventListener("webglcontextlost", (event) => {
console.log(event);
});
//WEBGL_lose_context 是 webgl 是属于 WebGLAPI 的一个扩展 API,它提供一组方法用来模拟一个 WebGLRenderingContext 上下文的丢失和恢复。
-gl.getExtension('WEBGL_lose_context').loseContext();
+gl.getExtension("WEBGL_lose_context").loseContext();
// "webglcontextlost" event is logged.
```
diff --git a/files/zh-cn/web/api/htmlcanvaselement/width/index.md b/files/zh-cn/web/api/htmlcanvaselement/width/index.md
index eeeb9643913eef..463e75c0f29f28 100644
--- a/files/zh-cn/web/api/htmlcanvaselement/width/index.md
+++ b/files/zh-cn/web/api/htmlcanvaselement/width/index.md
@@ -27,7 +27,7 @@ canvas.width = pxl;
你能够通过下面的代码得到 canvas 的宽度:
```js
-var canvas = document.getElementById('canvas');
+var canvas = document.getElementById("canvas");
console.log(canvas.width); // 300
```
diff --git a/files/zh-cn/web/api/htmlcollection/index.md b/files/zh-cn/web/api/htmlcollection/index.md
index b331ad96da8856..793df3949598af 100644
--- a/files/zh-cn/web/api/htmlcollection/index.md
+++ b/files/zh-cn/web/api/htmlcollection/index.md
@@ -19,11 +19,13 @@ HTML DOM 中的 `HTMLCollection` 是即时更新的(live);当其所包含
## 方法
- {{domxref("HTMLCollection.item()")}}
+
- : 根据给定的索引(从 0 开始),返回具体的节点。如果索引超出了范围,则返回 `null`。
访问 `collection[i]`(在索引 `i` 超出范围时会返回 `undefined`)的替代方法。这在非 JavaScript DOM 的实现中非常有用。
- {{domxref("HTMLCollection.namedItem()")}}
+
- : 根据 ID 返回指定节点,若不存在,则根据字符串所表示的 `name` 属性来匹配。根据 name 匹配只能作为最后的依赖,并且只有当被引用的元素支持 `name` 属性时才能被匹配。如果不存在符合给定 name 的节点,则返回 `null`。
访问 `collection[name]`(在 `name` 不存在时会返回 `undefined`)的替代方法。这在非 JavaScript DOM 的实现中非常有用。
diff --git a/files/zh-cn/web/api/htmlcollection/item/index.md b/files/zh-cn/web/api/htmlcollection/item/index.md
index 12c8ea34996a8b..128ced932312d2 100644
--- a/files/zh-cn/web/api/htmlcollection/item/index.md
+++ b/files/zh-cn/web/api/htmlcollection/item/index.md
@@ -23,9 +23,9 @@ HTMLCollection 中 `item()` 方法返回一个编号的元素,在 JavaScript
## Example
```js
-var c = document.images; // This is an HTMLCollection
-var img0 = c.item(0); // You can use the item( ) method this way
-var img1 = c[1]; // But this notation is easier and more common
+var c = document.images; // This is an HTMLCollection
+var img0 = c.item(0); // You can use the item( ) method this way
+var img1 = c[1]; // But this notation is easier and more common
```
## See also
diff --git a/files/zh-cn/web/api/htmlcollection/nameditem/index.md b/files/zh-cn/web/api/htmlcollection/nameditem/index.md
index 97b3484f90d033..1c3a9c6bdd2cb2 100644
--- a/files/zh-cn/web/api/htmlcollection/nameditem/index.md
+++ b/files/zh-cn/web/api/htmlcollection/nameditem/index.md
@@ -39,22 +39,22 @@ const item = collection.namedItem(key);
### JavaScript
```js
-const container = document.getElementById('personal');
+const container = document.getElementById("personal");
// Returns the HTMLSpanElement with the name "title" if no such element exists null is returned
-const titleSpan = container.children.namedItem('title');
+const titleSpan = container.children.namedItem("title");
// The following variants return undefined instead of null if there's no element with a matching name or id
-const firstnameSpan = container.children['firstname'];
+const firstnameSpan = container.children["firstname"];
const lastnameSpan = container.children.lastname;
// Returns the span element with the id "degree"
-const degreeSpan = container.children.namedItem('degree');
+const degreeSpan = container.children.namedItem("degree");
-const output = document.createElement('div');
+const output = document.createElement("div");
output.textContent = `Result: ${titleSpan.textContent} ${firstnameSpan.textContent} ${lastnameSpan.textContent} ${degreeSpan.textContent}`;
-container.insertAdjacentElement('afterend', output);
+container.insertAdjacentElement("afterend", output);
```
{{EmbedLiveSample("示例")}}
diff --git a/files/zh-cn/web/api/htmldetailselement/toggle_event/index.md b/files/zh-cn/web/api/htmldetailselement/toggle_event/index.md
index 6438efbcd878c5..a4daae2482f5dd 100644
--- a/files/zh-cn/web/api/htmldetailselement/toggle_event/index.md
+++ b/files/zh-cn/web/api/htmldetailselement/toggle_event/index.md
@@ -1,5 +1,5 @@
---
-title: 'HTMLDetailsElement: toggle event'
+title: "HTMLDetailsElement: toggle event"
slug: Web/API/HTMLDetailsElement/toggle_event
---
@@ -7,11 +7,11 @@ slug: Web/API/HTMLDetailsElement/toggle_event
当{{HtmlElement("details")}}元素`打开`/`关闭`状态被切换时,切换事件会触发。
-| Bubbles | No |
-| ---------------------- | ----------------------------------------------------------------------------- |
-| Cancelable | No |
-| Interface | {{DOMxRef("Event")}} |
-| Event handler property | None |
+| Bubbles | No |
+| ---------------------- | ------------------------------------------------------------------- |
+| Cancelable | No |
+| Interface | {{DOMxRef("Event")}} |
+| Event handler property | None |
| Default Action | Toggles the `open` state of the {{HtmlElement("details")}} element. |
## 示例
@@ -31,15 +31,18 @@ slug: Web/API/HTMLDetailsElement/toggle_event
Chapter summaries:
Chapter I
- Philosophy reproves Boethius for the foolishness of his complaints against Fortune. Her very nature is caprice.
+ Philosophy reproves Boethius for the foolishness of his complaints against
+ Fortune. Her very nature is caprice.
Chapter II
- Philosophy in Fortune's name replies to Boethius' reproaches, and proves that the gifts of Fortune are hers to give and to take away.
+ Philosophy in Fortune's name replies to Boethius' reproaches, and proves
+ that the gifts of Fortune are hers to give and to take away.
Chapter III
- Boethius falls back upon his present sense of misery. Philosophy reminds him of the brilliancy of his former fortunes.
+ Boethius falls back upon his present sense of misery. Philosophy reminds him
+ of the brilliancy of his former fortunes.
```
@@ -67,12 +70,12 @@ body {
```js
function logItem(e) {
const item = document.querySelector(`[data-id=${e.target.id}]`);
- item.toggleAttribute('hidden');
+ item.toggleAttribute("hidden");
}
-const chapters = document.querySelectorAll('details');
+const chapters = document.querySelectorAll("details");
chapters.forEach((chapter) => {
- chapter.addEventListener('toggle', logItem);
+ chapter.addEventListener("toggle", logItem);
});
```
diff --git a/files/zh-cn/web/api/htmldialogelement/index.md b/files/zh-cn/web/api/htmldialogelement/index.md
index 879de1db11eb5f..a2075006ad3eb1 100644
--- a/files/zh-cn/web/api/htmldialogelement/index.md
+++ b/files/zh-cn/web/api/htmldialogelement/index.md
@@ -13,19 +13,19 @@ slug: Web/API/HTMLDialogElement
_继承自父接口, {{domxref("HTMLElement")}}。_
-| 名称 | 类型 | 描述 |
-| ------------- | -------------------------------- | ------------------------------------------------------------------------------------------- |
-| `open` | {{domxref("Boolean")}} | 来自[`open`](/zh-CN/docs/Web/HTML/Element/dialog#open) HTML 属性,表示这个对话框可以进行互动。 |
-| `returnValue` | {{domxref("DOMString")}} | 用户获取对话框的值 |
+| 名称 | 类型 | 描述 |
+| ------------- | ------------------------ | ---------------------------------------------------------------------------------------------- |
+| `open` | {{domxref("Boolean")}} | 来自[`open`](/zh-CN/docs/Web/HTML/Element/dialog#open) HTML 属性,表示这个对话框可以进行互动。 |
+| `returnValue` | {{domxref("DOMString")}} | 用户获取对话框的值 |
## 方法
_继承自父接口, {{domxref("HTMLElement")}}。_
-| 名称和参数 | 类型 | 描述 |
-| ------------- | ------ | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
-| `close()` | `void` | 关闭对话框。可选传入类型为 {{domxref("DOMString")}} 的参数,用来更新对话框的 returnValue。 |
-| `show()` | `void` | 非模式化的显示这个对话框,即:打开这个对话框之后依然可以和其他内容进行交互。可选传入类型为 {{domxref("Element")}} 或者 {{domxref("MouseEvent")}} 的参数,用来定义对话框的显示位置。 |
+| 名称和参数 | 类型 | 描述 |
+| ------------- | ------ | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
+| `close()` | `void` | 关闭对话框。可选传入类型为 {{domxref("DOMString")}} 的参数,用来更新对话框的 returnValue。 |
+| `show()` | `void` | 非模式化的显示这个对话框,即:打开这个对话框之后依然可以和其他内容进行交互。可选传入类型为 {{domxref("Element")}} 或者 {{domxref("MouseEvent")}} 的参数,用来定义对话框的显示位置。 |
| `showModal()` | `void` | 模式化的显示这个对话框,并且将会至于所有其他对话框的顶层(屏蔽其他对话框的交互)。可选传入类型为{{domxref("Element")}} 或者 {{domxref("MouseEvent")}} 的参数,用来定义对话框的显示位置。 |
## Examples
@@ -39,10 +39,12 @@ _继承自父接口, {{domxref("HTMLElement")}}。_
- "Then art thou glad to seek repose?
- Art glad to leave the sea,
- And anchor all thy weary woes
- In calm Eternity?"
+
+ "Then art thou glad to seek repose?
+ Art glad to leave the sea,
+ And anchor all thy weary woes
+ In calm Eternity?"
+
```
@@ -71,13 +72,15 @@ _继承自父接口, {{domxref("HTMLElement")}}。_