javascript 在 asp.net 中更改标签文本

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20761122/
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-27 19:14:04  来源:igfitidea点击:

change label text in asp.net

javascriptasp.net

提问by user2958571

I'm trying to change label's text by using javascript:

我正在尝试使用javascript以下方法更改标签的文本:

<head runat="server">
    <script type="text/javascript">
        function updateLabel() {
            var lblElement = document.getElementbyId("Label2");
            lblElement.innerHtml("new");
        }
    </script>
</head>

I call this script from the code behind:

我从后面的代码中调用这个脚本:

mainPage.ClientScript.RegisterStartupScript(GetType(), "MyKey", "updateLabel();",true);

It's not working...

它不工作...

Using a buildin trigger like button click won't work for me that's whyI'm trying to find a way to do it with javascript.

使用像按钮点击这样的内置触发器对我不起作用,这就是为什么我试图找到一种使用 javascript 的方法。

回答by Ajay

Try this

试试这个

<script type="text/javascript">
   function updateLabel() {
       document.getElementById('Label2').innerHTML = 'New';
   }
</script>

回答by Amit

Try this

试试这个

<head runat="server">
 <script type="text/javascript">
    function updateLabel() {
        var lblElement = document.getElementbyId("Label2");
        lblElement.innerText="new";
    }
 </script>
</head>

回答by Manoj

Try this code:

试试这个代码:

JS:

JS

 <script type="text/javascript">
        function updateLabel()
          {
           document.getElementbyId("Label2").innerHTML= "new" ;
          }
    </script>

回答by geedubb

Javascript is CaSe SeNsItIvE, the property is innerHTMLNOT innerHtml, and it is a property not a method so you need lblElement.innerHTML = "new";. Also unless you are running .NET 4 or later and have ClientIDMode="Static" in your @pagedirective you will have to take into account the actual ID that is rendered in the HTML:

Javascript 是 Case SeNsItIvE,属性innerHTML不是innerHtml,并且它是属性而不是方法,因此您需要lblElement.innerHTML = "new";. 此外,除非您运行 .NET 4 或更高版本并且在您的@page指令中有 ClientIDMode="Static" ,否则您必须考虑在 HTML 中呈现的实际 ID:

<head runat="server">
    <script type="text/javascript">
        function updateLabel() {
            var lblElement = document.getElementbyId('<%=Label2.ClientID%>");
            lblElement.innerHTML = 'new';
        }
    </script>
</head>

回答by satya prakash

Try This code :

试试这个代码:

$('#lblId').text('text which you want to append dynemically');