使用 Javascript 设置 ASP 文字文本

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

Set ASP Literal text with Javascript

javascriptasp.netliterals

提问by James

I have an asp:Literal on my page (which cannot be converted to a Label or any other control) that I need to change the text of via JavaScript. I have the following code that works for a Label. Can anybody help?

我的页面上有一个 asp:Literal(无法转换为 Label 或任何其他控件),我需要通过 JavaScript 更改其文本。我有以下代码适用于Label。有人可以帮忙吗?

<script type="text/javascript">
        function changeText() {
            document.getElementById('<%= Test.ClientID %>').innerHTML = 'New Text';
        }
    </script>

        <a href="#" onclick='changeText()'>Change Text</a>
        <asp:Label id="Test" runat="server" Text="Original Text" />

Thanks

谢谢



UPDATE:更新:我无法从文字更改,因为背后的代码将 HTML/CSS 写入它以获取信息消息,例如:

LITMessage.Text = "<div class='success'>Information Successfully Updated</div>"

回答by SLaks

<asp:Literal>controls don't create their own HTML tag.
Therefore, there is no element that you can manipulate.

<asp:Literal>控件不创建自己的 HTML 标记。
因此,没有您可以操纵的元素。

Instead, you can wrap the <asp:Literal>in a <div>tag with an ID.

相反,您可以将 包装<asp:Literal><div>带有 ID的标签中。

回答by Justin Niessner

An ASP.NET Literal doesn't add any markup to the page. Therefore you have to wrap your content in some container so that you can edit it via JavaScript:

ASP.NET 文字不会向页面添加任何标记。因此,您必须将内容包装在某个容器中,以便您可以通过 JavaScript 对其进行编辑:

Assuming you had the following Literal on the page:

假设您在页面上有以下文字:

<asp:Literal runat="server" Id="literalControl" />

And were setting the text via code behind (because if you're not, you could just create the span/div in the markup to begin with and not have this issue):

并且是通过后面的代码设置文本(因为如果你不是,你可以在标记中创建 span/div 开始而不会有这个问题):

literalControl.Text = "Some text you want to change";

The code behind becomes:

后面的代码变成了:

literalControl.Text = "<span id='myId'>Some text you want to change</span>";

And the JavaScript would be:

JavaScript 将是:

document.getElementById('myId').innerHTML = 'New Text';

回答by atconway

Wrap the <asp:literal>control in a <div>and then use jQuery if needed to clear the contents like shown below:

<asp:literal>控件包裹在 a 中<div>,然后根据需要使用 jQuery 清除内容,如下所示:

<div id="divMyText">
  <asp:Literal ID="MyText" runat="server"></asp:Literal>
</div>

Here is how to clear the text using jQuery:

以下是使用 jQuery 清除文本的方法:

//Clear the html inside of the div
$("#divMyText").html("");

回答by stephen776

Does the literal contain html markup?

文字是否包含 html 标记?

if not, you could wrap the literal control in a div and give it an id. Then use js to replace the text within that div.

如果没有,您可以将文字控件包装在一个 div 中并给它一个 id。然后使用 js 替换该 div 中的文本。

in response to your update:

回应您的更新:

In that case, since you are rendering a div with a class of success, I would use jQuery to update the html in that div...it would be as simple as:

在这种情况下,由于您正在渲染一个类为 的 div success,我将使用 jQuery 来更新该 div 中的 html ......它会很简单:

$('.success').html('new html goes here');

回答by Ga???

A Literal is a direct render of text to the page. The only HTML it will render will be the HTML markup you include in the text string you set to the Literal. Instead of using a Literal surrounded by a div (unless you specifically want that functionality) you can use an ASP Labeland perform operations on it.

文字是将文本直接呈现到页面。它将呈现的唯一 HTML 将是您包含在您设置为 Literal 的文本字符串中的 HTML 标记。您可以使用ASP 标签并对其执行操作,而不是使用由 div 包围的文字(除非您特别想要该功能)。