想要将文字值访问到 javascript

Dib*_*iya 1 c# literals asp.net-4.0 c#-4.0

我在页面上有一个文字控件(上面有一些数据)。我想在 javascript 中访问它并想在上面放一些文本。请告诉我如何在 JavaScript 中访问文字控件。我正在尝试使用以下代码-

<asp:Literal  ID="lblPerInstanceListing" runat="server"></asp:Literal>
Run Code Online (Sandbox Code Playgroud)

Javascript:

 var value= document.getElementById('<%=lblPerInstanceListing.ClientID %>')
Run Code Online (Sandbox Code Playgroud)

我由此得到空值返回。

sco*_*rin 5

ASP.NET Literal 控件本身不会将任何 HTML 插入网页。您的文字控件是您将在后面的代码中设置的文本或 HTML 的占位符。

您应该将文字包装在 DIV 或 SPAN 中,并为其提供 ID 并在 JavaScript 中引用该 DIV 或 SPAN

网页表格:

<span id="yourId"><asp:Literal  ID="lblPerInstanceListing" runat="server"></asp:Literal></span>
Run Code Online (Sandbox Code Playgroud)

JavaScript: 由这段代码解决。

var value= document.getElementById('yourId').innerText;
Run Code Online (Sandbox Code Playgroud)