Disable Kanji Conversion with <input type="text">

Asked 1 years ago, Updated 1 years ago, 42 views

What should I do if I can't convert hiragana into kanji even if I press the conversion key with <input type="text"> in html?
If it's a space key, I can use Keycode, but what should I do with the conversion key?

javascript html

2022-09-30 20:50

1 Answers

It may not be a very good way to do this, but it worked like that.

 // Get the input element
var input = document.getElementById("input");

// Add an event listener to the input element
input.addEventListener("keyup", function(){
    // If the input value is composed only of [Ah-n], that is, if it is all in hiragana,
    if(this.value.match(/^[a-n]+$/){
        // out of focus
        This.blur();
        // return focus
        This.focus();
    }
});

If you look at the input value of input, and it's all hiragana, you'll lose focus for a moment by taking the focus and returning it, so that's when the input value is confirmed time.
All hiragana, for example, is because when you type kanji, the first k is not fixed.
That means it will only be confirmed when it becomes "ka".
The keyup specified in addEventListener is when the key is released.

However, with this method, we can't deal with cases where kanji is pasted with a copy paper.
If the value in the listener contains anything other than hiragana, it may be necessary to exclude it.

I hope I can be of service to you.

--Additional
Verified operation on Google Chrome 44.0.2403.155m


2022-09-30 20:50

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.