这个 jscript 会在 .ASCX 页面内运行吗?
它会在我的 ASP.Net 项目的 ASCX 文件中运行吗? 我似乎无法让它工作,只是想知道是否缺少一些特定的东西?我需要包含“runat=”server””吗?
<!--[if lte IE 6]>
<script type="text/javascript">
window.onload = function() {
var images = document.getElementById("GetQuote").getAttribute("ImageUrl");
for (var i = 0; i < images.length; i++) {
var image_png_src = images[i].src;
var image_gif_src = image_png_src.replace(".png", ".gif");
images[i].src = image_gif_src;
}
};
</script>
<![endif]-->
Will this run inside an ASCX file in my ASP.Net project?
I can't seem to get it to work, just wondered if there was some particular thing missing? Do i need to include "runat="server""?
<!--[if lte IE 6]>
<script type="text/javascript">
window.onload = function() {
var images = document.getElementById("GetQuote").getAttribute("ImageUrl");
for (var i = 0; i < images.length; i++) {
var image_png_src = images[i].src;
var image_gif_src = image_png_src.replace(".png", ".gif");
images[i].src = image_gif_src;
}
};
</script>
<![endif]-->
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
此 JavaScript 函数似乎正在尝试引用无法从客户端访问的 ASP.NET Web 控件属性。但是,您可以引用 ASP.NET 输出到页面的 HTML 实体及其属性。
假设您的 JavaScript 代码是 .ascx 代码中的代码,请将此行更改
为
:这样做的作用是插入 ASP.NET 为 GetQuote
Image
控件创建的客户端 ID,以便可以引用它从客户端。它还引用 HTMLimg
元素 (src
) 的正确属性,该属性对应于服务器端Image< 的
ImageUrl
属性。 /代码> 控制。编辑:
在看到 TheVillageIdiot 的响应(并更仔细地阅读您的代码,我最初应该这样做)后,我注意到您正在尝试将
images
变量用作数组。您可能正在尝试匹配 ID 中包含文本“GetQuote”的多个图像元素(例如 GetQuote1、GetQuote2 等)。假设您需要在客户端执行此操作,并且您没有使用像 jQuery 这样的框架,请尝试以下操作:
It appears that this JavaScript function is attempting to reference ASP.NET web control properties, which are not accessible from the client side. You can, however, reference the HTML entities that are output to the page by ASP.NET, along with their attributes.
Assuming your JavaScript code is code within the .ascx code, change this line:
To this:
What this does is insert the client ID that ASP.NET creates for the GetQuote
Image
control so that it can be referenced from the client side. It also references the proper attribute of the HTMLimg
element (src
) that corresponds to theImageUrl
property of the server sideImage
control.EDIT:
I noticed after seeing TheVillageIdiot's response (and reading your code a bit more closely, which I should have done initially) that you are trying to use the
images
variable as an array. It appears that you may be trying to match several image elements that contain the text "GetQuote" in their IDs (like GetQuote1, GetQuote2, etc.).Assuming that you need to do this on the client side, and that you are not using a framework like jQuery, try this:
您不需要 runat="server" 因为这是将在客户端上运行的代码。它应该可以工作,但也许您遇到问题是因为您引用的是 ASP.NET 控件项上的 ID?这意味着您的 ID 值不匹配。如果是这样,您可以通过使用 control.ClientID 重新渲染到 JavaScript 服务器端以使它们匹配来解决此问题。
You don't need a runat="server" because this is code that will run on the client. It should work, but maybe you are having problems because you are referencing IDs on items that are asp.net controls? This would mean that your ID values would not match. If so you could solve this by using control.ClientID redndered into the JavaScript server-side to make them match.
如果 GetQuote 是一个 aspx 元素,那么您需要将其替换为
<%= GetQuote.ClientID %>
并将ImageUrl
替换为src
就像图像也应该是一个字符串而不是字符串数组,所以你的循环也有问题。试试这个:
If GetQuote is an aspx element then you need to replace it with
<%= GetQuote.ClientID %>
andImageUrl
withsrc
likeAlso images should be one string not an array of strings so your loop is also at fault. Try this one instead: