我正在尝试将键盘事件传递给一个特殊的 HTML 元素类(mathquill span)。(按键在更高的级别上被捕获,所以我必须将它们向下喂。)这样做的标准方法似乎是使用 jQuery 的 .trigger,如前一个问题中所建议的那样。但是,该解决方案对我来说绝对没有任何作用。我终其一生都找不到问题所在。
此代码应该创建一个 MathQuill 框,然后在其中输入字母“f”。但盒子仍然是空的。
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" type="text/css" href="mathquill-master/mathquill.css">
</head>
<body>
<!-- loading external scripts -->
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
<script type="text/javascript" src="mathquill-master/mathquill.min.js"></script>
<!-- MathQuill box -->
<span class="mathquill-editable" id="mathquill_box"></span>
<script type="text/javascript">
$(function() {
$('.mathquill-editable').focus();
// creating 'fake' custom event (pressing the 'F' key)
// (this is taken directly from the answer to the previous question)
var customKeyDownEvent = $.Event('keydown');
customKeyDownEvent.bubbles = true;
customKeyDownEvent.cancelable = true;
customKeyDownEvent.charCode = 70; // key code for 'F'
customKeyDownEvent.keyCode = 70;
customKeyDownEvent.which = 70;
// send the event to the MathQuill box
$('.mathquill-editable textarea').trigger(customKeyDownEvent);
});
</script>
</body>
</html>
任何帮助将不胜感激!