javascript 如何在jsp页面动态改变标签框的文字?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13428100/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 18:44:31  来源:igfitidea点击:

How to change the text of the label box dynamically in jsp page?

javascriptjavajspjakarta-eedom-events

提问by Saravanan

In my jsp page, I have one button and one label. When page loading, the text of the label box is "Test". Suppose, user clicks the button then I have to change the text of the label into "tested".

在我的jsp 页面中,我有一个按钮和一个标签。页面加载时,标签框的文字为“Test”。假设,用户单击按钮,然后我必须将标签的文本更改为“已测试”。

I am new to Jsp.So, Please guide me to get out of this issue...

我是 Jsp 的新手。所以,请指导我摆脱这个问题......

回答by kieran

You would first need to set a listener to your button, say our markup was as follows:

你首先需要为你的按钮设置一个监听器,比如我们的标记如下:

<button id='btn'>test it</button>
<span id='label'>Test</span>

We can select the button element, and set it's onclick attribute:

我们可以选择按钮元素,并设置它的 onclick 属性:

document.getElementById('btn').onclick = test

Then we can define a function call testthat will toggle the label:

然后我们可以定义一个函数调用test来切换标签:

function test(){
 document.getElementById('label').innerHTML = 'Tested';
}

And that's it!

就是这样!

Here's a jsfiddledemonstrating this code.

这是演示此代码的jsfiddle

回答by polin

    <script type="text/javascript">
function fnc()
{
    document.getElementById("atext").innerHTML="tested";
}
</script>

<label id="atext">test</label>
<input type="button" onClick="fnc()" value="click me">