Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
187 views
in Technique[技术] by (71.8m points)

javascript - 双击html文本进行更改(Double click html text to change it)

I have a two columned HTML table which I want to translate from English to Greek.

(我有一个两列的HTML表,我想将其从英语翻译成希腊语。)


I desire to do that by double-clicking the Click to translate placeholder under the Greek column and then being prompted there, to insert a Greek text instead it.

(我希望通过双击“希腊语”列下的“ Click to translate占位符,然后在该提示下插入希腊文字来做到这一点。)


In other words, I would interact with the HTML file to translate where the placeholder, right from web browser;

(换句话说,我将与HTML文件进行交互以直接在Web浏览器中转换占位符。)

the changes will automatically get saved in the HTML file.

(更改将自动保存在HTML文件中。)

 table, th, td { border: 1px solid black; } 
 <!DOCTYPE html> <html> <body> <section class="allEnglishSections"> <h2>Conditioning</h2> <table> <tr><th>English</th><th>Greek</th></tr> <tr><td>if</td><td class="changeable">Click to translate</td></tr> <tr><td>than</td><td class="changeable">Click to translate</td></tr> <tr><td>else</td><td class="changeable">Click to translate</td></tr> </table> </section> <script> </script> </body> </html> 


From searching data on how to achieve this, I understand I should use something similar to this pseudocode:

(通过搜索有关如何实现此目的的数据,我了解到我应该使用类似于此伪代码的东西:)

document.querySelectorAll(".changeable").addEventListener(ondblclick, function() {
    myTranslation = prompt();
    document.querySelector(".changeable").value = myTranslation;
}

But it is unclear to me how to actually selecting all text of a cell by double clicking it, and then changing it directly from web browser in such a way that the HTML file would change.

(但是我不清楚如何实际选择一个单元格的所有文本,方法是双击它,然后直接从Web浏览器中更改它,以使HTML文件发生变化。)

Is this even possible with latest ECMAScript and if so how will you achieve this?

(最新的ECMAScript甚至有可能实现,如果是这样,您将如何实现?)

  ask by ShadowyShade translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

( UPDATED: See last section. )

((更新:请参阅最后一节。))

One way of doing this, is to create an object which maps english words to greek words, and then add an eventlistener that catches the double click event on the table element, checks if the 'click to translate' element has been clicked and do the translation:

(一种方法是创建一个将英语单词映射到希腊语单词的对象,然后添加一个事件侦听器,该事件侦听器捕获table元素上的双击事件,检查是否已单击“ click to translation”元素并执行翻译:)

 const dict = { if: "αν", than: "απ?", else: "αλλι??" } document.querySelector("table").addEventListener("dblclick", function(e){ // Check if the .changeable element has triggered the click event if ( e.target.classList.contains("changeable")){ // Get the text content from the sibling element which contains the english word const word = e.target.previousElementSibling.textContent; // Check if we have the word in our dictionary if ( dict[word] ){ // Change the text content of the 'click to translate' element with the greek word e.target.textContent = dict[word]; } } }); 
 table { -webkit-touch-callout: none; -webkit-user-select: none; -khtml-user-select: none; -moz-user-select: none; -ms-user-select: none; user-select: none; } 
  <section class="allEnglishSections"> <h2>Conditioning</h2> <table> <tr><th>English</th><th>Greek</th></tr> <tr><td>if</td><td class="changeable">Click to translate</td></tr> <tr><td>than</td><td class="changeable">Click to translate</td></tr> <tr><td>else</td><td class="changeable">Click to translate</td></tr> </table> </section> 

Updated

(更新)


Notes: As @Jhecht correcly pointed out, the querySelectorAll returns a NodeList that needs to be iterated.

(注意:正如@Jhecht正确指出的那样,querySelectorAll返回需要迭代的NodeList。)

The easiest way to do that, is through the forEach method available to the NodeList object.

(最简单的方法是通过NodeList对象可用的forEach方法。)

The code would be written like this:

(该代码将这样写:)

document.querySelectorAll(".changeable").forEach(function(element){

    element.addEventListener( "dblclick", function(){ /* CODE HERE */ } );  

});

If you see trying to attach multiple event listeners to a web page, you should reconsider your choice and instead attach a single event listener on a parent object which will be receiving all the events of its inner children.

(如果看到尝试将多个事件侦听器附加到网页,则应重新考虑选择,而将单个事件侦听器附加到将接收其内部子事件的所有事件的父对象上。)

I applied this pattern in my solution.

(我在解决方案中应用了这种模式。)


Since the question has been updated, and the intentions are more clear, I am adding another code that enables the user to double click on the Click to translate area and allow them to enter a custom value:

(由于问题已经更新,并且目的更加明确,因此我添加了另一个代码,该代码使用户可以双击“ Click to translate区域并允许他们输入自定义值:)

 document.querySelector("table").addEventListener("dblclick", function(e){ if ( e.target.classList.contains("changeable")){ e.target.setAttribute("contenteditable", true); if ( e.target.textContent === "Click to translate" ){ e.target.textContent = ""; e.target.focus(); } } }); 
 <section class="allEnglishSections"> <h2>Conditioning</h2> <table> <tr><th>English</th><th>Greek</th></tr> <tr><td>if</td><td class="changeable">Click to translate</td></tr> <tr><td>than</td><td class="changeable">Click to translate</td></tr> <tr><td>else</td><td class="changeable">Click to translate</td></tr> </table> </section> 


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...