1

我正在尝试从 javascript 将文本设置为 asp 标签,这是我尝试过的,但它不起作用

document.getElementById("Label1").value = "new text value";

 <asp:Label ID="Label1" name="Label1" Font-Size="XX-Large" runat="server" Text="I am just testing"></asp:Label>
4

7 回答 7

3

ASP.NET changes "Label1" to something like MasterPageContent_Label1 when rendered to the client. Also ASP.NET Label controls are renderd to the client as <span> elements so you need to use innerHTML as opposed to value to set the content.

document.getElementById('<%= Label1.ClientID %>').innerHTML = "new text value";
于 2012-09-06T11:29:36.253 回答
1

You need to get the ClientID of the control in order to manipulate it in JavaScript.

The ClientID is the Id that gets rendered in the browser.

document.getElementById("<%=Label1.ClientID%>").value = "new text value";
于 2012-09-06T11:28:58.150 回答
1

尝试这个 document.getElementById('<%= Label1.ClientID %>').InnerHTML = "Your Text Changed";

于 2012-09-06T11:29:58.587 回答
1

利用..

document.getElementById('<%=Label1.ClientID%>').innerText="New Text Value" ;
于 2012-09-06T11:30:26.683 回答
1

Label1是标签控件的服务器端 ID。使用ClientID从 javascript 访问它。尝试这个:

document.getElementById("<%=Label1.ClientID%>").innerHTML= "new text value";

希望这会有所帮助。

于 2012-09-06T11:30:30.660 回答
1

asp.net 标签呈现为跨度,因此您需要设置其 innerHTML 属性而不是 value 属性,另一种选择是使用 JQuery 并使用 .text() 方法

于 2012-09-06T11:31:07.913 回答
1

你可以试试这个: -

 document.getElementById("<%=Label1.ClientID%>").value = "new text value";

或者你可以试试

 var elMyElement = document.getElementByID('<%= Label1.ClientID %>');

  elMyElement.innerHTML = "your text here";
于 2012-11-07T19:06:58.443 回答