母版页Java脚本 [英] Masterpage & Javascript

查看:118
本文介绍了母版页Java脚本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

哦,很抱歉我的帖子不好.
这是我的问题:
当鼠标悬停在按钮上时,我使用了JavaScript代码来更改按钮的颜色.
我的所有页面都有按钮,因此我想创建一个母版页.
退出母版页并在网页中使用javascript代码即可正常工作.
但是当我创建母版页并在母版页中复制代码时,按钮的颜色不会更改,并且javascript代码无法正常工作.

我的页面(没有母版页)可以正常工作的代码是:

Oh, sorry for my bad post.
Here is the my problem:
I use a javascript code, for change color of button, when mouse go over the button.
I have buttons in all of my page, therefore I want create a masterpage.
Whit out masterpage and in a webpage, javascript code, work correctly.
But when I create masterpage and copy code in the masterpage, the color of the button doesn''t change and javascript code doesn''t work.

The code of my page(with out masterpage) that work correctly is:

<<pre lang="xml">%@ Page Language="C#" AutoEventWireup="true" CodeFile="with out master page.aspx.cs" Inherits="Default2" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <script language="JavaScript" type="text/JavaScript">
<!--
function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}
function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}
function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}
function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}
function obj1_onclick() {
}
//-->
    </script>
    <script language="javascript" type="text/javascript" for="obj1" event="onclick">
// <!CDATA[
return obj1_onclick()
// ]]>
    </script>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <td bgcolor="#999966" style="text-align: center">
    with out master page<br />
&nbsp;<asp:ImageButton ID="ImageButton3" runat="server" ImageUrl="~/menu7.gif" onMouseOut="MM_swapImgRestore()"
                            onMouseOver="MM_swapImage(''ImageButton3'','''',''menu_7.gif'',1)" AlternateText="onmouse"
                            ToolTip="onmouse" PostBackUrl="" meta:resourcekey="ImageButton3Resource1" />
    </div>
    </form>
</body>
</html>

推荐答案

我不知道它是如何工作的,或者这段代码在做什么,或者为什么它是如此复杂.但是:

onMouseOver ="MM_swapImage(''ImageButton3'',

这永远都行不通.您的按钮在客户端上不称为ImageButton3.您需要在后面的代码中使用按钮的ClientID属性,以获取实际的客户端ID,并将其注入到JS中.我这样做的方法是在JS中使用变量作为名称,然后进一步创建该变量并使用代码块读取内联ImageButton3.ClientID的值.
I don''t know how this ever works, or what this code is doing, or why it''s so complicated. However:

onMouseOver="MM_swapImage(''ImageButton3'',

This should never work. Your button is NOT called ImageButton3 on the client. You need to use the ClientID property of the button in your code behind to get the actual client side ID, and inject that in to your JS. The way I''d do that is to use a variable for the name in your JS, then further up, create that variable and use a code block to read the value of ImageButton3.ClientID, inline.


我的问题解决了.
在以下代码中:

My problem solved.
In the following code:

<pre lang="vb"><asp:ImageButton ID="ImageButton3" runat="server" ImageUrl="~/menu7.gif" onMouseOut="MM_swapImgRestore()"<br />
onMouseOver="MM_swapImage(''ImageButton3'','''',''menu_7.gif'',1)" AlternateText="onmouse"<br />
ToolTip="onmouse" PostBackUrl="" meta:resourcekey="ImageButton3Resource1" /</pre><br />
>



在为onMouseOver''事件调用的MM_swapImage()函数中,"ImageButton3"必须是"ctl00_ImageButton3":



In the MM_swapImage() function that is called for onMouseOver'' event , instead ''ImageButton3'' must be ''ctl00_ImageButton3'':

<code><pre lang="vb">&lt;asp:ImageButton ID=&quot;ImageButton3&quot; runat=&quot;server&quot; ImageUrl=&quot;~/menu7.gif&quot; onMouseOut=&quot;MM_swapImgRestore()&quot;
onMouseOver=&quot;MM_swapImage(&#39;ctl00_ImageButton3&#39;,&#39;&#39;,&#39;menu_7.gif&#39;,1)&quot; AlternateText=&quot;onmouse&quot;
ToolTip=&quot;onmouse&quot; PostBackUrl=&quot;&quot; meta:resourcekey=&quot;ImageButton3Resource1&quot; /</pre>
></code>



最好的问候,
尼玛(Nima)



Best regards,
Nima


这篇关于母版页Java脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆