这个 jscript 会在 .ASCX 页面内运行吗?

发布于 2024-08-07 23:20:00 字数 563 浏览 3 评论 0原文

它会在我的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

起风了 2024-08-14 23:20:00

此 JavaScript 函数似乎正在尝试引用无法从客户端访问的 ASP.NET Web 控件属性。但是,您可以引用 ASP.NET 输出到页面的 HTML 实体及其属性。

假设您的 JavaScript 代码是 .ascx 代码中的代码,请将此行更改

var images = document.getElementById("GetQuote").getAttribute("ImageUrl");

var images = document.getElementById('<%=GetQuote.ClientID%>').getAttribute("src");

:这样做的作用是插入 ASP.NET 为 GetQuote Image 控件创建的客户端 ID,以便可以引用它从客户端。它还引用 HTML img 元素 (src) 的正确属性,该属性对应于服务器端 Image< 的 ImageUrl 属性。 /代码> 控制。

编辑:
在看到 TheVillageIdiot 的响应(并更仔细地阅读您的代码,我最初应该这样做)后,我注意到您正在尝试将 images 变量用作数组。您可能正在尝试匹配 ID 中包含文本“GetQuote”的多个图像元素(例如 GetQuote1、GetQuote2 等)。

假设您需要在客户端执行此操作,并且您没有使用像 jQuery 这样的框架,请尝试以下操作:

window.onload = function() 
{        
    // get all img elements on the page and load them into an array
    var images = document.getElementsByTagName("img");

    // iterate through the image array
    for (var i = 0; i < images.length; i++) 
    {
        // check that the current image's id contains "GetQuote" (case sensitive)
        if (images[i].id.indexOf("GetQuote") >= 0)
        {
            var image_png_src = images[i].src;
            var image_gif_src = image_png_src.replace(".png", ".gif");
            images[i].src = image_gif_src;
        }
    }
};

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:

var images = document.getElementById("GetQuote").getAttribute("ImageUrl");

To this:

var images = document.getElementById('<%=GetQuote.ClientID%>').getAttribute("src");

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 HTML img element (src) that corresponds to the ImageUrl property of the server side Image 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:

window.onload = function() 
{        
    // get all img elements on the page and load them into an array
    var images = document.getElementsByTagName("img");

    // iterate through the image array
    for (var i = 0; i < images.length; i++) 
    {
        // check that the current image's id contains "GetQuote" (case sensitive)
        if (images[i].id.indexOf("GetQuote") >= 0)
        {
            var image_png_src = images[i].src;
            var image_gif_src = image_png_src.replace(".png", ".gif");
            images[i].src = image_gif_src;
        }
    }
};
欢烬 2024-08-14 23:20:00

您不需要 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.

只是我以为 2024-08-14 23:20:00

如果 GetQuote 是一个 aspx 元素,那么您需要将其替换为 <%= GetQuote.ClientID %> 并将 ImageUrl 替换为 src 就像

 var images = document.getElementById('<%=GetQuote.ClientID%>')
                                 .getAttribute("src");

图像也应该是一个字符串而不是字符串数组,所以你的循环也有问题。试试这个:

var image = document.getElementById('<%=GetQuote.ClientID%>').

if(images){
      var src = image.GetAttribute("src");
      image.SetAttribute("src",src.replace(".png", ".gif");
}

If GetQuote is an aspx element then you need to replace it with <%= GetQuote.ClientID %> and ImageUrl with src like

 var images = document.getElementById('<%=GetQuote.ClientID%>')
                                 .getAttribute("src");

Also images should be one string not an array of strings so your loop is also at fault. Try this one instead:

var image = document.getElementById('<%=GetQuote.ClientID%>').

if(images){
      var src = image.GetAttribute("src");
      image.SetAttribute("src",src.replace(".png", ".gif");
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文