简体   繁体   English

从.ascx文件调用Javascript函数

[英]Call Javascript Function from .ascx file


I have one javascript function named 'change2()' which is define in .ascx page's script tag. 我有一个名为“ change2()”的javascript函数,该函数在.ascx页面的脚本标签中定义。
I want to call that function from onclick event of img tag (Note : img tag is also on the same page). 我想从img标签的onclick事件调用该函数(注意:img标签也在同一页上)。
It is compulsory to use img tag only for image. 必须将img标签仅用于图像。
I tried all the below ways, but unfortunately It doesn't work for me. 我尝试了以下所有方法,但不幸的是,它对我不起作用。

Test.ascx Test.ascx

    <script language="javascript" type="text/javascript">
        function change2() {
            alert("Hi");
        } 
    </script>




  <table>
        <tr>
          <td class="list">
               Most liked
          </td>
          <td>
              <img id="imgLkU" src='<%# WebHelper.GetBaseURL(Request) + "/images/slide_btn_down.png"%>'class="icon_right_panel" runat="server" onclick="change2();" alt="Slider" />
           </td>
          </tr>
     </table>

Second Way : 第二种方式:

<table>
            <tr>
              <td class="list">
                   Most liked
              </td>
              <td>
                  <a href="#" onclick="change2();"><img id="imgLkU" src='<%# WebHelper.GetBaseURL(Request) + "/images/slide_btn_down.png"%>'class="icon_right_panel" runat="server"  alt="Slider" /></a>
               </td>
              </tr>
         </table>

Please give me your suggestions to call javascript function from same page. 请给我您的建议,以便从同一页面调用javascript函数。

As I can see, the img id is imgLkU , so, instead of including the call in the img tag itself, you can subscribe the event "from the outside", ie do it like using $.on , (or $.click ) like this: 如我所见, img id是imgLkU ,因此,您可以将事件“从外部”订阅,而不是在img标签本身中包括该调用,即像使用$ .on (或$ .click )一样进行操作像这样:

$.on('click','#imgLkU', function() { change2(); });
// or equivalent  $.on('click','#imgLkU', change2);

or 要么

$.('#imgLkU').click(function() { change2(); });
// or equivalent $.('#imgLkU').click(change2);

Do it right after defining change2 in the same script tag. 在同一脚本标签中定义change2之后change2执行此操作。

I'd also recommend you doing the change2 definition and the event subscription inside an inmediately invoked function expression to avoid polluting the global javascript namespace. 我还建议您在change2 调用的函数表达式中进行change2定义和事件订阅,以避免污染全局javascript名称空间。

(function() { 
   // define and subscribe here
 })();

Because your elements all have runat="server" , their onclick property is reserved for a backend-code actionlistener which will be executed at postback. 因为您的所有元素都具有runat="server" ,所以它们的onclick属性保留给后端代码actionlistener,它将在回发时执行。

the onClientClick property is reserved to allow you to still attach javascript "listeners" to what is considered the client-side onclick . 保留onClientClick属性,以允许您仍将javascript“侦听器”附加到客户端onclick

keep in mind that returning false from an onClientClick handler will prevent postback from happening if an onclick listener is also hooked up. 请记住,如果还连接了onclick侦听器, onClientClick处理函数返回false将防止发生回发。 ( onClientclick is executed before initiating the postback) onClientclick在启动回发之前执行)

try this : 尝试这个 :

<img id="imgLkU" src='<%# WebHelper.GetBaseURL(Request) + "/images/slide_btn_down.png"%>'class="icon_right_panel" runat="server" onclientclick="change2();" alt="Slider" />

The following function can be directly called from document.ready function like 可以从document.ready函数直接调用以下函数

$(function () {
    $('#imgLkU').click(function () {
        //do what ever you want
    })
});

First, take out the runat="server" on your image since you are already using server tags to set the url. 首先,因为您已经在使用服务器标记来设置URL,所以请在图像上删除runat="server" If you still want to use runat="server" , you can either: 如果仍然要使用runat="server" ,则可以:

1: change your img into an <asp:Image> tag and use ImageSource instead of src and OnClientClick instead of onclick . 1:将img更改为<asp:Image>标记,并使用ImageSource而不是srcOnClientClick而不是onclick

2: set the src attribute in the code behind. 2:在后面的代码中设置src属性。

After that, any click method - from your question to all the answers - should work. 之后,任何点击方法-从您的问题到所有答案-都应该起作用。

If that still does not show the alert, then start taking out code until it does and work your way from there... 如果仍然没有显示警报,则开始删除代码,直到出现警报为止,然后从那里开始工作。

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

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