简体   繁体   English

此jscript可以在.ASCX页面中运行吗?

[英]Will this jscript run inside a .ASCX page?

Will this run inside an ASCX file in my ASP.Net project? 它将在我的ASP.Net项目的ASCX文件中运行吗? 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""? 我是否需要包含“ 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]-->

It appears that this JavaScript function is attempting to reference ASP.NET web control properties, which are not accessible from the client side. 似乎此JavaScript函数正在尝试引用ASP.NET Web控件属性,这些属性无法从客户端进行访问。 You can, however, reference the HTML entities that are output to the page by ASP.NET, along with their attributes. 但是,您可以引用ASP.NET输出到页面的HTML实体及其属性。

Assuming your JavaScript code is code within the .ascx code, change this line: 假设您的JavaScript代码是.ascx代码中的代码,请更改此行:

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. 这样做是插入ASP.NET为GetQuote Image控件创建的客户端ID,以便可以从客户端引用它。 It also references the proper attribute of the HTML img element ( src ) that corresponds to the ImageUrl property of the server side Image control. 它还引用HTML img元素( src )的适当属性,该属性与服务器端Image控件的ImageUrl属性相对应。

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. 编辑:看到TheVillageIdiot的响应(并仔细阅读了您的代码后,我最初应该做的)后,我注意到您正在尝试将images变量用作数组。 It appears that you may be trying to match several image elements that contain the text "GetQuote" in their IDs (like GetQuote1, GetQuote2, etc.). 看来您可能正在尝试匹配其ID中包含文本“ GetQuote”的多个图像元素(例如GetQuote1,GetQuote2等)。

Assuming that you need to do this on the client side, and that you are not using a framework like jQuery, try this: 假设您需要在客户端执行此操作,并且未使用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;
        }
    }
};

You don't need a runat="server" because this is code that will run on the client. 您不需要runat =“ server”,因为这是将在客户端上运行的代码。 It should work, but maybe you are having problems because you are referencing IDs on items that are asp.net controls? 它应该可以工作,但是也许您遇到了问题,因为您引用的是作为asp.net控件的项目的ID? This would mean that your ID values would not match. 这意味着您的ID值将不匹配。 If so you could solve this by using control.ClientID redndered into the JavaScript server-side to make them match. 如果是这样,您可以通过使用control.ClientID修改到JavaScript服务器端以使其匹配来解决此问题。

If GetQuote is an aspx element then you need to replace it with <%= GetQuote.ClientID %> and ImageUrl with src like 如果GetQuote是aspx元素,则需要用<%= GetQuote.ClientID %>替换它,并用src替换ImageUrl ,例如

 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");
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM