How to change button text or link text in JavaScript?

tempy提出了一个问题:How to change button text or link text in JavaScript?,或许与您遇到的问题类似。

回答者I Hate Lazy给出了该问题的处理方式:

Change .text to .textContent to get/set the text content.

Or since you're dealing with a single text node, use in the same manner.

Also, let's make sensible use of a variable, and enjoy some code reduction and eliminate redundant DOM selection by caching the result of getElementById.

function toggleText(button_id) 
   var el = document.getElementById(button_id);
   if ( == "Lock") 
   { = "Unlock";
   { = "Lock";

Or even more compact like this:

function toggleText(button_id)  {
   var text = document.getElementById(button_id).firstChild; = == "Lock" ? "Unlock" : "Lock";