尝试重复函数以在输入间跳转

huangapple go评论75阅读模式
英文:

Trying to repeat functions to jump between inputs

问题

以下是您的JavaScript代码的中文翻译部分:

const ignoreOtherSymbols = document.querySelector(".word .first")
let counter = 1
let word = ''
let fifthValue = ''
let row = 0
let container = document.getElementsByClassName("word")[row]
let container2 = document.getElementsByClassName("word")[row]
let inputs = document.getElementsByClassName('first')[0] // class name as number in html and increment this when user lose in press enter

function isLetter(letter) {
    return /^[a-zA-Z]$/.test(letter)
}

ignoreOtherSymbols.addEventListener("keydown", function (event) {
    if (!isLetter(event.key)) {
        event.preventDefault()
    }
})

container.onkeyup = function(event) {
    let target = event.srcElement || event.target
    let myLength = target.value.length
    if (isLetter(event.key) && counter < 5) {
        word += target.value
    }
    if (event.key === 'Enter' && counter == 5) {
        row++
        fifthValue = target.value
        const wordArray = word.split('')
        wordArray[4] = fifthValue
        let newWord = wordArray.join('')
        console.log(row)
        console.log(newWord)
        const apiUrl = 'https://words.dev-apis.com/word-of-the-day?puzzle=1337'
        fetch(apiUrl)
        .then(response => response.json())
        .then(data => {
            console.log(data);
            if (data.word === newWord) {
                console.log('You win')
            }
            else {
                console.log('Try again')
                console.log(row)
                if (document.getElementsByClassName("word")[row]) {
                    counter = 1
                    word = ''
                    fifthValue = ''
                    // I need to connect this if else (mylength) to jump between inputs with the same row
                    inputs.focus()
                }
            }
        })
        .catch(error => {
            console.error(error)
        })
    }
    if (event.key === 'Backspace') {
        target.value = ''
    }
    if (myLength === 1) {
        while (target = target.nextElementSibling) {
            if (target.tagName.toLowerCase() === "input") {
                if (isLetter(event.key)) {
                    target.focus()
                    counter++
                }
                break
            }
        }
    }
    console.log(counter)
    console.log(word)
}

container2.onkeydown = function(event) {
    let target = event.srcElement || event.target
    let myLength = target.value.length
    if (isLetter(event.key) && counter === 5) {
        target.value = target.value.slice(0, -1)
    }
    if (myLength === 0) {
        while (target = target.previousElementSibling) {
            if (target.tagName.toLowerCase() === "input") {
                if (event.key === 'Backspace') {
                    target.focus()
                    counter--
                    target.value = ''
                    word = word.slice(0, -1)
                }
                break
            }
        }
    }
}

希望这能帮助您理解代码并解决您的问题。如果您需要更多帮助,请随时提出。

英文:

const ignoreOtherSymbols = document.querySelector(&quot;.word .first&quot;)
let counter = 1
let word = &#39;&#39;
let fifthValue = &#39;&#39;
let row = 0
let container = document.getElementsByClassName(&quot;word&quot;)[row]
let container2 = document.getElementsByClassName(&quot;word&quot;)[row]
let inputs = document.getElementsByClassName(&#39;first&#39;)[0] // class name as number in html and increment this when user lose in press enter
function isLetter(letter) {
    return /^[a-zA-Z]$/.test(letter)
  }
  ignoreOtherSymbols.addEventListener(&quot;keydown&quot;, function (event) {
  if (!isLetter(event.key)) {
    event.preventDefault()
  }
})
container.onkeyup = function(event) {
    let target = event.srcElement || event.target
    let myLength = target.value.length
    if (isLetter(event.key) &amp;&amp; counter &lt; 5) {
        word += target.value
    }
    if (event.key === &#39;Enter&#39; &amp;&amp; counter == 5) {
        row++
        fifthValue = target.value
        const wordArray = word.split(&#39;&#39;)
        wordArray[4] = fifthValue
        let newWord = wordArray.join(&#39;&#39;)
        console.log(row)
        console.log(newWord)
        const apiUrl = &#39;https://words.dev-apis.com/word-of-the-day?puzzle=1337&#39;
        fetch(apiUrl)
        .then(response =&gt; response.json())
        .then(data =&gt; {
        console.log(data);
        if (data.word === newWord) {
            console.log(&#39;You win&#39;)
        }
        else {
            console.log(&#39;Try again&#39;)
            console.log(row)
            if (document.getElementsByClassName(&quot;word&quot;)[row]) {
                counter = 1
                word = &#39;&#39;
                fifthValue = &#39;&#39;
                 // I need to connect this if else (mylength) to jump between inputs with the same row
                inputs.focus()
            }
        }
        })
        .catch(error =&gt; {
            console.error(error)
        })
        }
    if (event.key === &#39;Backspace&#39;) {
        target.value = &#39;&#39;
    }
    if (myLength === 1) {
        while (target = target.nextElementSibling) {
            if (target.tagName.toLowerCase() === &quot;input&quot;) {
                if (isLetter(event.key)) {
                    target.focus()
                    counter++
                }
                break
            }
        }
    }
    console.log(counter)
    console.log(word)
}
container2.onkeydown = function(event) {
    let target = event.srcElement || event.target
    let myLength = target.value.length
    if (isLetter(event.key) &amp;&amp; counter === 5) {
        target.value = target.value.slice(0, -1)
    }
    if (myLength === 0) {
        while (target = target.previousElementSibling) {
            if (target.tagName.toLowerCase() === &quot;input&quot;) {
                if (event.key === &#39;Backspace&#39;) {
                    target.focus()
                    counter--
                    target.value = &#39;&#39;
                    word = word.slice(0, -1)
                }
                break
            }
     }
    }
}

&lt;!DOCTYPE html&gt;
&lt;html lang=&quot;en&quot;&gt;
    &lt;head&gt;
        &lt;meta charset=&quot;UTF-8&quot; /&gt; &lt;!--for emoticons, ASCII don&#39;t give emoticons--&gt;
        &lt;meta name=&quot;viewport&quot; content=&quot;width=device-width, initial-scale.0&quot; /&gt; &lt;!--for view side on smartphone--&gt;
        &lt;title&gt;Word Master&lt;/title&gt; &lt;!--title of my side--&gt;
        &lt;link rel=&quot;stylesheet&quot; href=&quot;index.css&quot; /&gt;
      &lt;/head&gt;
      &lt;body class=&quot;page&quot;&gt;
        &lt;h1 class=&quot;logo&quot;&gt;Word Masters&lt;/h1&gt;
        &lt;div class=&quot;line&quot;&gt;&lt;/div&gt;
        &lt;div class=&quot;nothing&quot;&gt;&lt;/div&gt;
        &lt;div class=&quot;letter&quot;&gt;
            &lt;div class=&quot;firstWord word&quot;&gt;
                &lt;input type=&quot;text&quot; maxlength=&quot;1&quot;&gt;
                &lt;input type=&quot;text&quot; maxlength=&quot;1&quot;&gt;
                &lt;input type=&quot;text&quot; maxlength=&quot;1&quot;&gt;
                &lt;input type=&quot;text&quot; maxlength=&quot;1&quot;&gt;
                &lt;input type=&quot;text&quot; maxlength=&quot;1&quot;&gt;
            &lt;/div&gt;
            &lt;div class=&quot;secondWord word&quot;&gt;
                &lt;input type=&quot;text&quot; maxlength=&quot;1&quot; class=&quot;first&quot;&gt;
                &lt;input type=&quot;text&quot; maxlength=&quot;1&quot; class=&quot;first&quot;&gt;
                &lt;input type=&quot;text&quot; maxlength=&quot;1&quot; class=&quot;first&quot;&gt;
                &lt;input type=&quot;text&quot; maxlength=&quot;1&quot; class=&quot;first&quot;&gt;
                &lt;input type=&quot;text&quot; maxlength=&quot;1&quot; class=&quot;first&quot;&gt;
            &lt;/div&gt;
            &lt;div class=&quot;thirdWord word&quot;&gt;
                &lt;input type=&quot;text&quot; maxlength=&quot;1&quot; class=&quot;first&quot;&gt;
                &lt;input type=&quot;text&quot; maxlength=&quot;1&quot; class=&quot;first&quot;&gt;
                &lt;input type=&quot;text&quot; maxlength=&quot;1&quot;&gt;
                &lt;input type=&quot;text&quot; maxlength=&quot;1&quot;&gt;
                &lt;input type=&quot;text&quot; maxlength=&quot;1&quot;&gt;
            &lt;/div&gt;
            &lt;div class=&quot;fourthWord word&quot;&gt;
                &lt;input maxlength=&quot;1&quot;&gt;
                &lt;input maxlength=&quot;1&quot;&gt;
                &lt;input maxlength=&quot;1&quot;&gt;
                &lt;input maxlength=&quot;1&quot;&gt;
                &lt;input maxlength=&quot;1&quot;&gt;
            &lt;/div&gt;
            &lt;div class=&quot;fifthWord word&quot;&gt;
                &lt;input maxlength=&quot;1&quot;&gt;
                &lt;input maxlength=&quot;1&quot;&gt;
                &lt;input maxlength=&quot;1&quot;&gt;
                &lt;input maxlength=&quot;1&quot;&gt;
                &lt;input maxlength=&quot;1&quot;&gt;
            &lt;/div&gt;
            &lt;div class=&quot;sixthWord word&quot;&gt;
                &lt;input maxlength=&quot;1&quot;&gt;
                &lt;input maxlength=&quot;1&quot;&gt;
                &lt;input maxlength=&quot;1&quot;&gt;
                &lt;input maxlength=&quot;1&quot;&gt;
                &lt;input maxlength=&quot;1&quot;&gt;
            &lt;/div&gt;
        &lt;/div&gt;
        &lt;script src=&quot;./wordMaster.js&quot;&gt;&lt;/script&gt;
      &lt;/body&gt;
&lt;/html&gt;

I try to increment number of row if user press 'Enter' to start again the same what is going in container and container2. My result is that in first row everything is ok, but when focus jump to first input in second row, focus of my input don't change when user press letter (when myLength = 1). I need some advice how to resolve this. Thank you a lot for help.

答案1

得分: 0

以下是您要求的代码部分的翻译:

const ignoreOtherSymbols = document.querySelector(".word .first");
let counter = 1;
let word = '';
let fifthValue = '';
let row = 0;

function isLetter(letter) {
    return /^[a-zA-Z]$/.test(letter);
}

ignoreOtherSymbols.addEventListener("keydown", function (event) {
    if (!isLetter(event.key)) {
        event.preventDefault();
    }
});

addKeyUpEventListener(row); // 在行 0 添加事件监听器

function addKeyUpEventListener(row) {
    console.log('row', row);
    let inputs = document.getElementsByClassName('first')[row]; // HTML 中的类名对应行数,当用户按下回车键时递增
    let container = document.getElementsByClassName("word")[row];
    container.onkeyup = function (event) {
        let target = event.srcElement || event.target;
        let myLength = target.value.length;
        if (isLetter(event.key) && counter < 5) {
            word += target.value;
        }
        if (event.key === 'Enter' && counter === 5) {
            row++;
            fifthValue = target.value;
            const wordArray = word.split('');
            wordArray[4] = fifthValue;
            let newWord = wordArray.join('');
            console.log(row);
            console.log(newWord);
            const apiUrl = 'https://words.dev-apis.com/word-of-the-day?puzzle=1337';
            fetch(apiUrl)
                .then(response => response.json())
                .then(data => {
                    console.log(data);
                    if (data.word === newWord) {
                        console.log('You win');
                    } else {
                        console.log('Try again');
                        console.log(row);
                        if (document.getElementsByClassName("word")[row]) {
                            counter = 1;
                            word = '';
                            fifthValue = '';
                            inputs.focus();
                        }
                    }
                    addKeyUpEventListener(row); // 在行 n 添加事件监听器
                })
                .catch(error => {
                    console.error(error);
                });
        }
        if (event.key === 'Backspace') {
            target.value = '';
        }
        if (myLength === 1) {
            while (target = target.nextElementSibling) {
                if (target.tagName.toLowerCase() === "input") {
                    if (isLetter(event.key)) {
                        target.focus();
                        counter++;
                    }
                    break;
                }
            }
        }
        console.log(counter);
        console.log(word);
    }
}

请注意,我已经将您提供的代码翻译成了中文,并根据您的要求省略了任何额外的内容。如果您有任何其他翻译需求,请随时提出。

英文:

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

const ignoreOtherSymbols = document.querySelector(&quot;.word .first&quot;)
let counter = 1
let word = &#39;&#39;
let fifthValue = &#39;&#39;
let row = 0
function isLetter(letter) {
return /^[a-zA-Z]$/.test(letter)
}
ignoreOtherSymbols.addEventListener(&quot;keydown&quot;, function (event) {
if (!isLetter(event.key)) {
event.preventDefault()
}
})
addKeyUpEventListner(row); // add eventlistner at row 0
function addKeyUpEventListner(row) {
console.log(&#39;row&#39;, row)
let inputs = document.getElementsByClassName(&#39;first&#39;)[row] // class name as number in html and increment this when user lose in press enter
let container = document.getElementsByClassName(&quot;word&quot;)[row]
container.onkeyup = function(event) {
let target = event.srcElement || event.target
let myLength = target.value.length
if (isLetter(event.key) &amp;&amp; counter &lt; 5) {
word += target.value
}
if (event.key === &#39;Enter&#39; &amp;&amp; counter == 5) {
row++
fifthValue = target.value
const wordArray = word.split(&#39;&#39;)
wordArray[4] = fifthValue
let newWord = wordArray.join(&#39;&#39;)
console.log(row)
console.log(newWord)
const apiUrl = &#39;https://words.dev-apis.com/word-of-the-day?puzzle=1337&#39;
fetch(apiUrl)
.then(response =&gt; response.json())
.then(data =&gt; {
console.log(data);
if (data.word === newWord) {
console.log(&#39;You win&#39;)
}
else {
console.log(&#39;Try again&#39;)
console.log(row)
if (document.getElementsByClassName(&quot;word&quot;)[row]) {
counter = 1
word = &#39;&#39;
fifthValue = &#39;&#39;
// I need to connect this if else (mylength) to jump between inputs with the same row
inputs.focus()
}
}
addKeyUpEventListner(row);  // add eventlistner at row n
})
.catch(error =&gt; {
console.error(error)
})
}
if (event.key === &#39;Backspace&#39;) {
target.value = &#39;&#39;
}
if (myLength === 1) {
while (target = target.nextElementSibling) {
if (target.tagName.toLowerCase() === &quot;input&quot;) {
if (isLetter(event.key)) {
target.focus()
counter++
}
break
}
}
}
console.log(counter)
console.log(word)
}
}

<!-- language: lang-html -->

&lt;h1 class=&quot;logo&quot;&gt;Word Masters&lt;/h1&gt;
&lt;div class=&quot;line&quot;&gt;&lt;/div&gt;
&lt;div class=&quot;nothing&quot;&gt;&lt;/div&gt;
&lt;div class=&quot;letter&quot;&gt;
&lt;div class=&quot;firstWord word&quot;&gt;
&lt;input type=&quot;text&quot; maxlength=&quot;1&quot;&gt;
&lt;input type=&quot;text&quot; maxlength=&quot;1&quot;&gt;
&lt;input type=&quot;text&quot; maxlength=&quot;1&quot;&gt;
&lt;input type=&quot;text&quot; maxlength=&quot;1&quot;&gt;
&lt;input type=&quot;text&quot; maxlength=&quot;1&quot;&gt;
&lt;/div&gt;
&lt;div class=&quot;secondWord word&quot;&gt;
&lt;input type=&quot;text&quot; maxlength=&quot;1&quot; class=&quot;first&quot;&gt;
&lt;input type=&quot;text&quot; maxlength=&quot;1&quot;&gt;
&lt;input type=&quot;text&quot; maxlength=&quot;1&quot;&gt;
&lt;input type=&quot;text&quot; maxlength=&quot;1&quot;&gt;
&lt;input type=&quot;text&quot; maxlength=&quot;1&quot;&gt;
&lt;/div&gt;
&lt;div class=&quot;thirdWord word&quot;&gt;
&lt;input type=&quot;text&quot; maxlength=&quot;1&quot; class=&quot;first&quot;&gt;
&lt;input type=&quot;text&quot; maxlength=&quot;1&quot;&gt;
&lt;input type=&quot;text&quot; maxlength=&quot;1&quot;&gt;
&lt;input type=&quot;text&quot; maxlength=&quot;1&quot;&gt;
&lt;input type=&quot;text&quot; maxlength=&quot;1&quot;&gt;
&lt;/div&gt;
&lt;div class=&quot;fourthWord word&quot;&gt;
&lt;input maxlength=&quot;1&quot; class=&quot;first&quot;&gt;
&lt;input maxlength=&quot;1&quot;&gt;
&lt;input maxlength=&quot;1&quot;&gt;
&lt;input maxlength=&quot;1&quot;&gt;
&lt;input maxlength=&quot;1&quot;&gt;
&lt;/div&gt;
&lt;div class=&quot;fifthWord word&quot;&gt;
&lt;input maxlength=&quot;1&quot; class=&quot;first&quot;&gt;
&lt;input maxlength=&quot;1&quot;&gt;
&lt;input maxlength=&quot;1&quot;&gt;
&lt;input maxlength=&quot;1&quot;&gt;
&lt;input maxlength=&quot;1&quot;&gt;
&lt;/div&gt;
&lt;div class=&quot;sixthWord word&quot;&gt;
&lt;input maxlength=&quot;1&quot; class=&quot;first&quot;&gt;
&lt;input maxlength=&quot;1&quot;&gt;
&lt;input maxlength=&quot;1&quot;&gt;
&lt;input maxlength=&quot;1&quot;&gt;
&lt;input maxlength=&quot;1&quot;&gt;
&lt;/div&gt;
&lt;/div&gt;

<!-- end snippet -->

separate addEventlistener with row as a parameter as a function

huangapple
  • 本文由 发表于 2023年4月20日 09:45:43
  • 转载请务必保留本文链接:https://go.coder-hub.com/76059961.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定