SVGGraphicsElement: paste event - Web APIs 编辑

The paste event is fired on an SVGGraphicsElement when the user has initiated a "paste" action through the browser's user interface.

BubblesYes
CancelableYes
InterfaceClipboardEvent
Event handler propertyonpaste

If the cursor is in an editable context (for example, in a <textarea> or an element with contenteditable attribute set to true) then the default action is to insert the contents of the clipboard into the document at the cursor position.

A handler for this event can access the clipboard contents by calling getData() on the event's clipboardData property.

To override the default behavior (for example to insert some different data or a transformation of the clipboard contents) an event handler must cancel the default action using event.preventDefault(), and then insert its desired data manually.

It's possible to construct and dispatch a synthetic paste event, but this will not affect the document's contents.

Example

HTML

<?xml version="1.0" encoding="UTF-8"?>
<svg viewBox="0 0 140 30" width="600" height="320" xmlns="http://www.w3.org/2000/svg">
    <foreignObject x="5" y="-10" width="90" height="20">
        <input xmlns="http://www.w3.org/1999/xhtml" value="Copy this text"/>
    </foreignObject>
    <text x="5" y="30" id="element-to-paste-text" tabindex="1">Paste it here</text>
</svg>

CSS

input {
  font-size: 10px;
  width: 100%;
  height: 90%;
  box-sizing: border-box;
  border: 1px solid black;
}

JavaScript

document.getElementById("element-to-paste-text").addEventListener("paste", evt => {
  evt.target.textContent = evt.clipboardData.getData("text/plain").toUpperCase();
  evt.preventDefault();
});

Result

Specifications

SpecificationStatusComment
Scalable Vector Graphics (SVG) 2Candidate RecommendationDefinition that the clipboard events apply to SVG elements.
Clipboard API and eventsWorking DraftInitial definition

See also

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据

词条统计

浏览:62 次

字数:5590

最后编辑:8年前

编辑次数:0 次

    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文