ContextMenu 中默认选中的输入文本(焦点)

Selected input text by default (focus) in ContextMenu

我有一个由上下文菜单生成的文本字段,我希望默认选择它(默认自动对焦)。我用函数 jquery focus [$('.classInput') 没有成功。焦点()]

$(function(){
        $.contextMenu({
            selector: '.input-context-menu', 
            items: {
                // <input type="text">
                name_input: {
                    name: "Name input :", 
                    type: 'text', 
                    value: "value_input",
                    events: {
                        keyup: function(e) {
...

events.show 部分设置 focus
在设置 focus.
之前,您需要 timeoutinput 元素出现在 DOM 中 input 名称将以 context-menu-input-.

为前缀

查看下面的代码片段:

$(function() {
  $.contextMenu({
    selector: '.context-menu-one',
    items: {
      // <input type="text">
      input1: {
        name: "Text",
        type: 'text',
        value: "Hello World",
        events: {
          keyup: function(e) {
            window.console && console.log('key: ' + e.keyCode);
          }
        }
      }
    },
    events: {
      show: function(opt) {
        var $this = this;
        $.contextMenu.setInputValues(opt, $this.data());
        
        // Set focus to the input element
        setTimeout(() => {
          $('[name="context-menu-input-input1"]').focus();
        }, 10);
      },
      hide: function(opt) {
        var $this = this;
        $.contextMenu.getInputValues(opt, $this.data());
      }
    }
  });
});
<link href="https://swisnl.github.io/jQuery-contextMenu/dist/jquery.contextMenu.css" rel="stylesheet" type="text/css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://swisnl.github.io/jQuery-contextMenu/dist/jquery.contextMenu.js" type="text/javascript"></script>


<span class="context-menu-one btn btn-neutral">right click me</span>

希望对您有所帮助。