gpt4 book ai didi

javascript - 将键盘事件附加到 HTML 中的 SVG 元素

转载 作者:数据小太阳 更新时间:2023-10-29 04:44:53 25 4
gpt4 key购买 nike

当在浏览器中运行 HTML 内的 SVG 时,无论如何要将 textInput 或 keypress(或实际上任何其他文本输入)事件附加到 SVG 元素?

我可以将事件处理程序附加到 document.documentElement,但是当我通过任何机制将事件处理程序附加到任何类型的 SVGElement 时,没有任何反应......SVG,g,rect......

我从 SVG 规范中看到这不受支持 ( W3 SVG list of events ),但是是否有任何浏览器支持任何“附加功能”?

我无法在 Chrome 或 Firefox 上运行以下内容...

<!DOCTYPE HTML>
<html>
<head>
<title>Key event test</title>
<style type="text/css">
body,html {
margin: 0;
padding: 0;
height: 100%;
width: 100%;
background: #f00;
}

#main {
margin: 0;
padding: 0;
height: 100%;
width: 80%;
background: #0f0;
float: right;
}

#drawArea {
margin: 0;
padding: 0;
height: 100%;
width: 100%;
background: #ffffff;
}

#side {
margin: 0;
padding: 0;
height: 100%;
width: 20%;
background: #00f;
float: left;
}


</style>
<script type="text/javascript">

function createBoundEventHandler(el, name) {
var f =
function eventHandler(ev) {
if("mousedown" === ev.type) {
el.focus();
}
else {
alert("asEventHandler: " + el.localName + " " + name + " " + ev.type);
}
return true;
};
return f;
}

function doEvent(event, elementName, eventName) {
alert("asAttribute: " + elementName + " " + eventName + " " + event.type);
return true;
}

function addEventHandler(element, eventName, type) {
var attrName = "on" + eventName;
var attrValue = "doEvent(evt, '" + element.localName + "', '" + eventName + "')";
if("asAttribute" === type) {
element.setAttribute( attrName, attrValue);
}
else {
element.addEventListener(eventName, createBoundEventHandler(element, eventName), false);
}
}

window.onload = function() {
var eventHandlerType = "asEventHandler"; //asAttribute asEventHandler

var svgTarget = document.getElementById('drawArea');
var svgRect = document.getElementById('aRect');
var nonSVG = document.getElementById('side');

addEventHandler(svgTarget, "keypress", eventHandlerType);
addEventHandler(svgTarget, "mousedown", eventHandlerType);
addEventHandler(svgTarget, "keyup", eventHandlerType);
addEventHandler(svgTarget, "keydown", eventHandlerType);

addEventHandler(svgRect, "keypress", eventHandlerType);
addEventHandler(svgRect, "mousedown", eventHandlerType);
addEventHandler(svgRect, "keyup", eventHandlerType);
addEventHandler(svgRect, "keydown", eventHandlerType);

addEventHandler(nonSVG, "keypress", eventHandlerType);
addEventHandler(nonSVG, "mousedown", eventHandlerType);
addEventHandler(nonSVG, "keyup", eventHandlerType);
addEventHandler(nonSVG, "keydown", eventHandlerType);

//We can get a keypress if we attach an event handler to the underlying document Element
addEventHandler(document.documentElement, "keypress", eventHandlerType);
}
</script>
</head>
<body>
<div id="side" tabindex="0">
</div>
<div id="main">
<svg version="1.1" xmlns="http://www.w3.org/2000/svg" id="drawArea" focusable="true" tabindex="0">
<rect x="0" y="0" width="100" height="100"
style="fill:yellow; ;stroke-width:2; stroke:rgb(0,0,0)" id="aRect" focusable="true"/>
</svg>
</div>
</body>
</html>

如示例所示,我仍然可以回退到“背景”doc 元素上的关键事件。有没有人有任何 pet 库,以及将其与 SVGElement 相关联的技术。

注意我也试过 tabindex(0) 并 focus 没有效果......

最佳答案

正如用户 Erik Dahlström 所建议的,您(首先)需要为焦点添加一个事件监听器。

svgRect.addEventListener('focus', function(){
this.addEventListener('keypress',function(e){
console.log(e.keyCode);
});
}, svgRect);

关于javascript - 将键盘事件附加到 HTML 中的 SVG 元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6747848/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com