gpt4 book ai didi

javascript - 坚持使用 JavaScript 函数?

转载 作者:行者123 更新时间:2023-11-28 03:55:14 26 4
gpt4 key购买 nike

我必须调用一个函数将法语短语翻译成英语,但我卡在了 showEnglish() 上。它不会拉取 english 数组。而且我什至不确定我是否引用了我在单独的 JavaScript 文件中的设置函数。

这是我目前所拥有的:

window.onload = setup;
function setup() {
var questions = document.querySelectorAll("article ol li");
for (var i = 0; i < questions.length; i++){
questions[i].id = i + "phrase";
questions[i].onmousedown = showEnglish;
//questions[i].onmouseup = showFrench;
questions[i].style.cursor = "pointer";
}
}
function showEnglish() {
var phrases = document.getElementById("phrase");
var phraseNumber = parseInt(phrases.id);
phraseNumber.innerHTML = english[phraseNumber];
phraseNumber.style.fontStyle = "italic";
phraseNumber.style.fontColor = "rgb(191, 22, 31)";
}

最佳答案

这是一个不使用 ID 的示例代码段,应该符合您的需求。

Questions list can easily be built from translations object using a script. (Check snippet in second solution below)

// On document load (warning: does not work with IE8)
document.addEventListener('DOMContentLoaded', function() {
setup()
})

// Store French -> English translations
var translations = {
"Comment vous appelez vous ?": "What's your name ?",
"Où habitez vous ?": "Where do you live ?"
};

// Attach `translate` function to questions `mousedown` event
function setup() {

document
.querySelectorAll("article ol li")
.forEach(function(el) {
el.onmousedown = showEnglish
});
}

// Display translation when mousedown on element
function showEnglish(event) {
var element = event.srcElement || event.target
var translation = document.getElementById("translation");
translation.innerHTML = translations[element.innerText];
}
<html>
<head>
<style>
.question { cursor: pointer; padding: 4px }
.translation { font-style: italic; color: rgb(191, 22, 31) }
</style>
</head>
<body>
<h3>Questions</h3>
<article>
<ol>
<li class="question">Comment vous appelez vous ?</li>
<li class="question">Où habitez vous ?</li>
</ol>
</article>
<h3>Traduction</h3>
<div class="translation" id="translation"></div>
</body>
</html>

其他解决方案

从翻译构建 DOM

// On document load (warning: does not work with IE8)
document.addEventListener('DOMContentLoaded', function() {
buildDOM();
})

// Store French -> English translations
var translations = {
"Comment vous appelez vous ?": "What's your name ?",
"Où habitez vous ?": "Where do you live ?"
};

// Display translation when mousedown on element
function showEnglish(event) {
var element = event.srcElement || event.target
var translation = document.getElementById("translation");
translation.innerHTML = translations[element.innerText];
}

function buildDOM() {
var ol = document.createElement("ol");

for (var t in translations) {
var el = document.createElement("li");
el.className = "question";
el.innerText = t;
el.onmousedown = showEnglish
ol.appendChild(el);
}

document.getElementById("questions").appendChild(ol);
}
<html>
<head>
<style>
.question { cursor: pointer; padding: 4px }
.translation { font-style: italic; color: rgb(191, 22, 31) }
</style>
</head>
<body>
<h3>Questions</h3>
<div id="questions"></div>
<h3>Traduction</h3>
<div id="translation" class="translation"></div>
</body>
</html>

关于javascript - 坚持使用 JavaScript 函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43506346/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com