Javascript 如何在javascript中访问文字控件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2226864/
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
How to access literal control in javascript
提问by Dr. Rajesh Rolen
I have got a literal control on page (with some data on it). i want to access it in javascript and want to put some text on it. please tell me how can i access literal control in javascript. (i am using asp.net)
我在页面上有一个文字控件(上面有一些数据)。我想在 javascript 中访问它并想在上面放一些文本。请告诉我如何在 javascript 中访问文字控件。(我正在使用asp.net)
My code in javascript (but not working):
我在 javascript 中的代码(但不工作):
lths = document.getElementById("<%= lblhs.ClientID %>");
lths.innerHTML = 'this is text line"
回答by Yuriy Faktorovich
You can find it using document.getElementById('<%= myControl.ClientID %>')or if you are using .NET 4.0 you can directly set the id field to be pulled by javascript. Alternatively you can use jquery's selectors. Your code isn't working because the Literal control doesn't render as an element, just static text. You can place a span around it, or use a Label control, which renders as a span for you.
您可以使用它找到它,document.getElementById('<%= myControl.ClientID %>')或者如果您使用的是 .NET 4.0,您可以直接设置要由 javascript 拉取的 id 字段。或者,您可以使用jquery 的选择器。您的代码不起作用,因为 Literal 控件不呈现为元素,只是静态文本。你可以在它周围放置一个跨度,或者使用一个标签控件,它为你呈现一个跨度。
回答by Andre Kraemer
I don't think that you can access a literal control from your javascript client code because a literal control just renders it's pure value without any surrounding tags. So you don't really have an element which you could get by id.
我不认为您可以从您的 javascript 客户端代码访问文字控件,因为文字控件只是呈现它的纯值而没有任何周围的标签。因此,您实际上没有可以通过 id 获取的元素。
What you could to is changing the literal control to a label control. This renders out as a html span tag by default. Like that you can select it on the client side.
您可以将文字控件更改为标签控件。默认情况下,这会呈现为 html span 标签。像这样,您可以在客户端选择它。
Another option would be to write a span or div tag in your literal control on the server side like this:
另一种选择是在服务器端的文字控件中编写一个 span 或 div 标签,如下所示:
myLiteral.Text = "<div id=\"myDiv\">Some Text here</div>"
on the client you could select the elemnt by using:
在客户端上,您可以使用以下方法选择元素:
var myDiv = document.getElementById('myDiv');
回答by Brian Moeskau
Just to rule out the obvious (since no one else mentioned it), this is a syntax error in JS:
只是为了排除明显的(因为没有其他人提到它),这是 JS 中的语法错误:
lths.innerHTML = 'this is text line"
Not sure if this was a typing error here, or if you copied it from your code. You can use either "or 'but not both to surround a string. Also, you should use terminating semi-colons as best practice (although it's not required).
不确定这是否是此处的输入错误,或者您是否从代码中复制了它。您可以使用一个"或'两个来包围一个字符串,但不能同时使用。此外,您应该使用终止分号作为最佳实践(尽管这不是必需的)。
lths.innerHTML = 'this is text line';
回答by Aseem Gautam
The <%= myControl.ClientId %>technique does not work when you are using master pages. That could be the case.
What you can do is: Take a hidden field. Save the ID of the label in the hidden field. Access the value attribute to get the ID and then use document.getElementByID('HiddenField.Value')to get the control.
<%= myControl.ClientId %>当您使用母版页时,该技术不起作用。情况可能是这样。你可以做的是: 取一个隐藏的字段。将标签的 ID 保存在隐藏字段中。访问 value 属性获取 ID,然后使用document.getElementByID('HiddenField.Value')获取控件。

