[英]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: 一些东西:
click
that won't work. click
命名为无效。 document.getElementById
takes string as argument, so you want to write there document.getElementById('btnaccept');
document.getElementById
将string作为参数,因此你想在那里写document.getElementById('btnaccept');
=
to assign value ===
is comparison (equal value or type) =
来赋值===
是比较(等值或类型) 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";
}
}
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.