我试图将键盘事件传递给一个特殊的HTML元素类(mathquill span)。(按键在更高的级别被捕获,所以我必须用匙喂它们。)实现这一点的标准方法似乎是使用this previous question中提出的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>任何帮助都将不胜感激!
发布于 2016-01-11 04:38:57
使用mathquill内置的write方法在当前光标位置插入latex。例如:
$('.mathquill-editable').mathquill('write', 'x^2')请参考my previous answer了解如何在之后对其进行聚焦。
https://stackoverflow.com/questions/31527746
复制相似问题