【发布时间】:2018-07-03 14:06:17
【问题描述】:
我正在编写一个可拖动的 html div 为:
它看起来像这样:
但是我不能输入像普通输入 div 这样的东西:<input />。
它不响应任何关键事件。
我已尝试使用stopPropagation 将事件停止给它的父母:
input.onclick = function ( evt ) {
evt.stopPropagation();
console.log( 'input got clicked' );
};
$( input ).on( 'keydown', function ( evt ) {
evt.stopPropagation();
console.log( 'input got keydown' );
} );
input 在哪里:
let input = document.createElement( 'input' );
input.setAttribute( 'type', 'text' );
和console.log( input ):
但这无济于事。 (而对于后面的 keydown 事件,控制台中不会给出任何输出。)
谁能建议我调试这个问题的方法?它真的让我发疯。谢谢!
PS:使用的是 Chrome。
更新:我找到了问题,但不知道原因。
是因为我把父dom装饰成jquery draggable,需要取消<input class='x-leaf'/>为:
$('#input-parent').draggable({
containment: 'window',
cancel: '.x-leaf'
});
用之前的stopPropogation和@Brainfeeder建议的方式,终于奏效了。
【问题讨论】:
-
向我们展示您如何在
$( input )中定义input。 -
$('input')
-
@Cruiser 正确,除非他已将输入定义为变量,如果已定义,仍应以其他方式完成
-
您一定遇到了错误,
input in not defined -
感谢您的回复。
input是一个变量,我在问题中对其进行了编辑。
标签: javascript jquery html css dom