$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>wp editor - How can I apply preventDefault() to the click on CodeMirror-hints?|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

wp editor - How can I apply preventDefault() to the click on CodeMirror-hints?

matteradmin9PV0评论

If I click on a selection in the autocompleter of WordPress in codeMirror, the whole field is closed. To prevent this I want to add preventDefault to the click event.

var cm = wp.codeEditor.initialize( options.field, editorSettings );
var editor = cm.codemirror;

jQuery('body').hover(function(e){
          if(jQuery('ul.CodeMirror-hints')){
            jQuery('ul.CodeMirror-hints').mousedown(function(eH){
              eH.preventDefault();
            });
          }
        });

var editorSettings = {
        mode: 'css',
        lineNumbers: true,
        indentUnit: 2,
        tabSize: 2,
        autoRefresh:true,
        autocorrect: true,
        autocapitalize: true,
        matchBrackets: true,
        autoCloseBrackets: true,
        lineWrapping: true,
        lint: true,
        gutters: [ 'CodeMirror-lint-markers' ]
    }

Since ul.CodeMirror-hints is now stored in the body element and is only inserted when codeMirror hints makes a suggestion, I have to make an event on body.

body => <ul class="CodeMirror-hints"...</ul>

Is there a better solution to add preventFefault() to the event click on ul.CodeMirror-hints?

If I click on a selection in the autocompleter of WordPress in codeMirror, the whole field is closed. To prevent this I want to add preventDefault to the click event.

var cm = wp.codeEditor.initialize( options.field, editorSettings );
var editor = cm.codemirror;

jQuery('body').hover(function(e){
          if(jQuery('ul.CodeMirror-hints')){
            jQuery('ul.CodeMirror-hints').mousedown(function(eH){
              eH.preventDefault();
            });
          }
        });

var editorSettings = {
        mode: 'css',
        lineNumbers: true,
        indentUnit: 2,
        tabSize: 2,
        autoRefresh:true,
        autocorrect: true,
        autocapitalize: true,
        matchBrackets: true,
        autoCloseBrackets: true,
        lineWrapping: true,
        lint: true,
        gutters: [ 'CodeMirror-lint-markers' ]
    }

Since ul.CodeMirror-hints is now stored in the body element and is only inserted when codeMirror hints makes a suggestion, I have to make an event on body.

body => <ul class="CodeMirror-hints"...</ul>

Is there a better solution to add preventFefault() to the event click on ul.CodeMirror-hints?

Share Improve this question asked Feb 27, 2019 at 8:55 SeverinSeverin 551 gold badge1 silver badge8 bronze badges
Add a comment  | 

2 Answers 2

Reset to default 0

Event delegation is your friend! This way the element doesn't have to exist when the code runs to have the function bound to it.

jQuery('body').on('click', 'ul.CodeMirror-hints li', function(e) {
    e.preventDefault();
});

replace click with mousedown if you need to

This solution is not beautiful but it does what it should.

jQuery('body').hover(function(e){
          if( jQuery('ul.CodeMirror-hints li').length > 0){
            jQuery('ul.CodeMirror-hints li').mousedown(function(eH){
              eH.preventDefault();
            });
            jQuery('ul.CodeMirror-hints li').click(function(ecH){
              ecH.preventDefault();
            });
          }
Post a comment

comment list (0)

  1. No comments so far