このページはコミュニティーの尽力で英語から翻訳されました。MDN Web Docs コミュニティーについてもっと知り、仲間になるにはこちらから。

View in English Always switch to English

DataTransferItem: kind プロパティ

Baseline Widely available

This feature is well established and works across many devices and browser versions. It’s been available across browsers since ⁨2016年11月⁩.

DataTransferItem.kind プロパティは読み取り専用で、 ドラッグデータ項目 の種類(テキストまたはファイル)を表す DataTransferItem オブジェクトを返すものです。

ドラッグデータ項目の種類を表す文字列です。以下の値のいずれかでなければなりません。

'file'

ドラッグデータ項目がファイルの場合。

'string'

ドラッグデータ項目の種類が プレーン Unicode 文字列 である場合。

この例では、 kind プロパティを使用しています。

js
function dropHandler(ev) {
  console.log("Drop");
  ev.preventDefault();
  for (const item of ev.dataTransfer.items) {
    if (item.kind === "string" && item.type.match("^text/plain")) {
      // This item is the target node
      item.getAsString((s) => {
        ev.target.appendChild(document.getElementById(s));
      });
    } else if (item.kind === "string" && item.type.match("^text/html")) {
      // Drag data item is HTML
      console.log("… Drop: HTML");
    } else if (item.kind === "file" && item.type.match("^image/")) {
      // Drag data item is an image file
      const f = item.getAsFile();
      console.log("… Drop: File");
    }
  }
}

仕様書

Specification
HTML
# dom-datatransferitem-kind-dev

ブラウザーの互換性

関連情報