简体   繁体   English

通过单击JavaScript更改按钮文本

[英]Changing button text by Click in JavaScript

I'm using following codes in a JSP page.This is my function to change button text by a button click in JavaScript. 我在JSP页面中使用以下代码。这是我通过JavaScript中的按钮单击更改按钮文本的功能。

    <script>
    function click(){
        var el=document.getElementById(btnaccept);
        if(el.value=="Accept"){
           el.value==="Accepted"; 
        }else{
            el.value==="Accept";
        }
    } 
 </script>

This is my button; 这是我的按钮;

<button type="button" class="btn btn-default btn-sm" id="btnaccept"  onclick="click()">
                <span class="glyphicon glyphicon-ok" aria-hidden="true" name="accept"></span> Accept
      </button>

This code is not functioning when I click the "Accept" button. 单击“接受”按钮时,此代码无法正常工作。 What error I have done here? 我在这做了什么错误?

Try this code 试试这个代码

<script>
      function click1() {
        var el = document.getElementById('btnaccept');
        //alert(el.innerHTML);
        if (el.textContent == "Accept") {
          el.textContent = "Accepted";
        } else {
          el.textContent = "Accept";
        }
      }
    </script>

change onclick event to click1() 将onclick事件更改为click1()

A few things: 一些东西:

  • Don't name your funtion click that won't work. 不要将您的功能click命名为无效。
  • You are not using value on the button, you are using inner HTML. 您没有在按钮上使用值,而是使用内部HTML。
  • use if you want to manipulate the value, or manipulate ineerHTML if you want the other way around. 如果你想要操纵值,请使用,或者如果你想要反过来操纵ineerHTML。
  • document.getElementById takes string as argument, so you want to write there document.getElementById('btnaccept'); document.getElementById将string作为参数,因此你想在那里写document.getElementById('btnaccept');
  • Use = to assign value === is comparison (equal value or type) 使用=来赋值===是比较(等值或类型)

See working demo 看工作演示

First thing you should use click as function name use any other name. 首先应该使用click作为函数名称使用任何其他名称。 It will treat it as mouseevent docs 它会将其视为mouseevent 文档

=== is used for comparison to assign value use = . ===用于比较分配值use =

Here I have used clickbutton() instead of click() for the function name. 这里我使用clickbutton()而不是click()作为函数名。

HTML HTML

<button type="button" class="btn btn-default btn-sm" id="btnaccept"  onclick="clickbutton()">

Script 脚本

function clickbutton() {
   var el = document.getElementById('btnaccept');
   if (el.value == "Accept") {
     el.value = "Accepted";
   } else {
     el.value = "Accept";
   }
 }

Demo here 在这里演示

As mattt said, you need to use the assignment operator = instead of the equality operator === when assigning values. 正如mattt所说,在分配值时,您需要使用赋值运算符=而不是等于运算符=== Also, when using getElementById , it will be expecting a string, so you want to use getElementById("btnaccept"); 另外,当使用getElementById ,它会期望一个字符串,所以你想使用getElementById("btnaccept");

As well, to set the text of a button element I believe you will need to use el.innerText = "Accept"; 同样,要设置按钮元素的文本,我相信你需要使用el.innerText = "Accept";

I implemented a toggle button for a colapsible panel in the following way: 我通过以下方式为可压缩面板实现了一个切换按钮:

<button type="button" class="btn btn-default" onclick="changeIcon(this)">
<span class="glyphicon glyphicon-triangle-bottom"></span>
</button>

<script>
function changeIcon(button) {

    if(button.firstElementChild.getAttribute("class") == "glyphicon glyphicon-triangle-bottom")
    {
        button.firstElementChild.setAttribute("class", "glyphicon glyphicon-triangle-top");
    }
    else
    {
        button.firstElementChild.setAttribute("class", "glyphicon glyphicon-triangle-bottom");
    }
}
</script>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM