用于表和查找表的Javascript

时间:2015-06-18 04:09:48

标签: jquery html css javascript-events lookup-tables

我试图建立一个网站,每个单元格中都有一个包含中文字符的表格。用户单击单个单元格,每个单元格中的字符显示在表格旁边的文本框中。在查找表中找到字符序列,在第二个文本框中提供英语翻译。 这是HTML:

<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>                    

这就是CSS:

type="text/css">                                                                                            
.table  
    {
        border-collapse:collapse;
        border-spacing:0;
    }                                                                               
.table  td
    {
        font-family:Verdana,sans-serif;
        font-size:19px;
        padding:10px 5px;
        border-style:solid;
        border-width:1px;
        overflow:hidden;
        word-break:normal;
    }
.table  
    .red    {color:#fe0000}                                                                                             
    .green  {color:#32cb00}                                                                                         
    .yellow {color:#ffc702}

我的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"};

(请注意,并非所有点击组合都会产生英文翻译。)

我遇到的问题是:

  1. 创建将检测点击并发送单元格的事件侦听器 内容到中文文本框
  2. 使用查找表和
  3. 在中文框中查找文本
  4. 在英语翻译框中显示结果。
  5. 非常感谢任何帮助。特别是代码!

    由于

1 个答案:

答案 0 :(得分:0)

为结果添加几个元素:

<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);

摆弄代码:
http://jsfiddle.net/6wjeuymj/1/

请注意,包含类名称(例如:<div class=translator-puzzle> ... </div>)的表(和结果元素)周围的包装器将使您能够通过有效地编写更清晰(更安全)的代码依赖于类而不是ID的可重用组件。