Jquery 表和查找表的Javascript
我正试图建立一个网站,有一个表,每个单元格包含一个汉字。用户单击各个单元格,每个单元格中的字符将显示在表格旁边的文本框中。在查找表中可以找到字符序列,在第二个文本框中提供英文翻译。 以下是HTML:Jquery 表和查找表的Javascript,jquery,html,css,javascript-events,lookup-tables,Jquery,Html,Css,Javascript Events,Lookup Tables,我正试图建立一个网站,有一个表,每个单元格包含一个汉字。用户单击各个单元格,每个单元格中的字符将显示在表格旁边的文本框中。在查找表中可以找到字符序列,在第二个文本框中提供英文翻译。 以下是HTML: <html> <head> <head> <title> Practice translation </title> <script src="http://code.
<html>
<head>
<head>
<title>
Practice translation
</title>
<script
src="http://code.jquery.com/jquery-1.9.1.min.js">
</script>
<script
type="text/javascript"
src="Small1.js">
</script>
<meta
http-equiv="Content-Type"
content="text/html; charset=UTF-8">
<link
rel="shortcut icon"
href="/favicon.ico"
type="image/x-icon">
<link
rel="stylesheet"
type="text/css"
href="Small1.css">
<link
rel="shortcut icon"
href="Pallindrome.ico"
type="image/x-icon">
<link
rel="icon"
href="Pallindrome.ico"
type="image/x-icon">
</head>
</head>
<body>
<table style="text-align:center">
<tbody>
<tr>
<td id= “M13” style="text-align:center" title="“duān“ end, extreme; head; beginning" class="yellow">端</td>
<td id= “N13” style="text-align:center" title="“wú, mó“ negative, no, not; KangXi radical 71" class="yellow">无</td>
<td id= “O13” style="text-align:center" title="“zhōng“ end; finally, in the end" color="#f1c232" class="yellow">终</td>
<td id= “P13” style="text-align:center" title="“shǐ“ begin, start; then, only then" class="yellow">始</td>
<td id= “Q13” style="text-align:center" title="“shī“ poetry; poem, verse, ode"class="yellow">诗</td>
</tr>
<tr>
<td id= “M14” style="text-align:center" title="“bǐ, bì, pí, pǐ“ to compare, liken; comparison; than" class="yellow">比</td>
<td id= “N14” style="text-align:center" title="“píng“ flat, level, even; peaceful" class="green" >平</td>
<td id= “O14” style="text-align:center" title="“shǐ“ begin, start; then, only then" class="green">始</td>
<td id= “P14” style="text-align:center" title="“xuán“ beautiful jade; star" class="green">璇</td>
<td id= “Q14” style="text-align:center" title="“qíng“ feeling, sentiment, emotion" class="yellow">情</td>
</tr>
<tr>
<td id= “M15” style="text-align:center" title="“zuò, zuō, zuó“ make; work; compose, write; act, perform" class="yellow">作</td>
<td id= “N15” style="text-align:center" title="“sū“ revive, resurrect; a species of thyme" class="green">苏</td>
<td id= “O15” style="text-align:center" title="“xīn“ heart; mind, intelligence; soul" class="red" >心</td>
<td id= “P15” style="text-align:center" title="“jī“ pearl that is not quite round" class="green">玑</td>
<td id= “Q15” style="text-align:center" title="“míng“ bright, light, brilliant; clear" class="yellow">明</td>
</tr>
<tr>
<td id= “M16” style="text-align:center" title="“lì, lí“ beautiful, magnificent, elegant" class="yellow">丽</td>
<td id= “N16” style="text-align:center" title="“shì, zhī, jīng“ clan, family; mister" class="green">氏</ td>
<td id= “O16” style="text-align:center" title="“shī“ poetry; poem, verse, ode" class="green">诗</td>
<td id= “P16” style="text-align:center" title="“tú“ diagram; chart, map, picture" class="green" >图</td>
<td id= “Q16” style="text-align:center" title="“xiǎn“ manifest, display; evident, clear" class="yellow" >显 </td>
</tr>
<tr>
<td id= “M17” style="text-align:center" title="“cí“ words, speech, expression, phrase" class="yellow" >辞</ td>
<td id= “N17” style="text-align:center" title="“lǐ“ reason, logic; manage" class="yellow" >理</td>
<td id= “O17” style="text-align:center" title="“xīng, xìng“ thrive, prosper, flourish" class="yellow" >兴</ td>
<td id= “P17” style="text-align:center" title="“yì“ right conduct, righteousness" class="yellow" >义</td>
<td id= “Q17” style="text-align:center" title="“yuàn“ hatred, enmity, resentment" class="yellow" >怨</td>
</tr>
</tbody>
</table>
<br>
我拥有的JAVAScript查找表是:
var lookupTable = {
"端无终始诗":"The beginning of this poem starts at the end,",
"比平始璇情":"It begins peacefully like a smooth jade star",
"作苏心玑明":"Its creation revived my twisted heart",
"丽氏诗图显":"Clearly this star-chart poem is a classic",
"辞理兴义怨":"Knowledge thrives where good calls out evil",
"端比作丽辞":"The beginning of this work is like a great speech"};
(请注意,并非所有单击组合都会生成英文翻译)
我遇到的问题是:
谢谢为结果添加一些元素:
<div id=output></div>
<div id=translation></div>
function processClick(event) {
var elem = $('#output');
elem.text(elem.text() + $(event.target).text());
$('#translation').text(lookupTable[elem.text()]);
}
$(document).on('click', 'td', processClick);
篡改代码:请注意,使用类名(例如:
…
)围绕表(和结果元素)的包装器,可以有效地使可重用组件依赖于类而不是ID,从而编写更干净(更安全)的代码。为结果添加两个元素:
<div id=output></div>
<div id=translation></div>
function processClick(event) {
var elem = $('#output');
elem.text(elem.text() + $(event.target).text());
$('#translation').text(lookupTable[elem.text()]);
}
$(document).on('click', 'td', processClick);
篡改代码:请注意,使用类名(例如:
…
)围绕表(和结果元素)的包装器,可以有效地使可重用组件依赖于类而不是ID来编写更干净(更安全)的代码。$(“.classofcell”)。on(“单击”,function(){//code here})代码>-这是事件委派!你可以用这个<代码>$(“.classofcell”)。在(“单击”,函数(){//code here})上代码>-这是事件委派!你可以用这个<代码>$(“.classofcell”)。在(“单击”,函数(){//code here})上代码>-这是事件委派!你可以用这个!