我正在尝试从 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>
我正在尝试从 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>
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";
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";
尝试这个 document.getElementById('<%= Label1.ClientID %>').InnerHTML = "Your Text Changed";
利用..
document.getElementById('<%=Label1.ClientID%>').innerText="New Text Value" ;
Label1
是标签控件的服务器端 ID。使用ClientID
从 javascript 访问它。尝试这个:
document.getElementById("<%=Label1.ClientID%>").innerHTML= "new text value";
希望这会有所帮助。
asp.net 标签呈现为跨度,因此您需要设置其 innerHTML 属性而不是 value 属性,另一种选择是使用 JQuery 并使用 .text() 方法
你可以试试这个: -
document.getElementById("<%=Label1.ClientID%>").value = "new text value";
或者你可以试试
var elMyElement = document.getElementByID('<%= Label1.ClientID %>');
elMyElement.innerHTML = "your text here";