C# 为什么不是';这个脚本不是从母版页运行的吗?
我有一个与母版页关联的页面。在母版页中,我将css链接放在head部分,并放置jquery脚本标记和包含用于切换网格的函数的脚本,但当它不起作用时。当我点击一个项目时,它看起来甚至没有调用showhide 以下是母版页的一个片段:C# 为什么不是';这个脚本不是从母版页运行的吗?,c#,asp.net,javascript,jquery,C#,Asp.net,Javascript,Jquery,我有一个与母版页关联的页面。在母版页中,我将css链接放在head部分,并放置jquery脚本标记和包含用于切换网格的函数的脚本,但当它不起作用时。当我点击一个项目时,它看起来甚至没有调用showhide 以下是母版页的一个片段: <head runat="server"> <title></title> <link href="MainLayout.css" rel="stylesheet" type="text/css" /> <link
<head runat="server">
<title></title>
<link href="MainLayout.css" rel="stylesheet" type="text/css" />
<link href="ajaxtab.css" rel="stylesheet" type="text/css" />
<link href="dialog.css" rel="stylesheet" type="text/css" />
<link href="grid.css" rel="stylesheet" type="text/css" />
<link href="pager.css" rel="stylesheet" type="text/css" />
<link href="subgrid.css" rel="stylesheet" type="text/css" />
<script src="jquery-1.3.2.min.js" type="text/javascript"></script>
<script type="text/javascript">
//master: id of div element that contains the information about master data
//details: id of div element wrapping the details grid
function showhide(master, detail) {
//First child of master div is the image
var src = $(master).children()[0].src;
//Switch image from (+) to (-) or vice versa.
if (src.endsWith("plus.png"))
src = src.replace('plus.png', 'minus.png');
else
src = src.replace('minus.png', 'plus.png');
//Set new image
$(master).children()[0].src = src;
//Toggle expand/collapse
$(detail).slideToggle("normal");
}
</script>
<asp:ContentPlaceHolder ID="head" runat="server">
</asp:ContentPlaceHolder>
</head>
//master:包含主数据信息的div元素的id
//详细信息:包装详细信息网格的div元素的id
函数showhide(主、细节){
//主div的第一个子级是图像
var src=$(主).children()[0].src;
//将图像从(+)切换到(-),反之亦然。
if(src.endsWith(“plus.png”))
src=src.replace('plus.png','minus.png');
其他的
src=src.replace('minus.png','plus.png');
//树立新形象
$(主).children()[0].src=src;
//切换展开/折叠
$(细节)。滑动切换(“正常”);
}
以下是在aspx页面的onclick事件中包含showhide函数的div:
<div class="searchgroup"
id='<%#String.Format("master{0}",Container.DataItemIndex)%>'
onclick='showhide(<%#String.Format("\"#master{0}\"",Container.DataItemIndex)%>
,
<%#String.Format("\"#detail{0}\"",Container.DataItemIndex) %>)'>
<asp:Image ID="imgCollapsible"
CssClass="first"
ImageUrl="plus.png"
Style="margin-right: 5px;"
runat="server" />
<span class="searchheader"><%#Eval("Business")%></span>
</div>
以下是它为master和detail div生成的html:
//master div
<div class="searchgroup"
id='master0'
onclick='showhide("#master0","#detail0")'>
<img id="ctl00_ContentPanel_gvMaster_ctl02_imgCollapsible"
class="first" src="plus.png"
style="border-width:0px;margin-right: 5px;" />
<span class="searchheader">ABC</span>
</div>
//details div
<div id='detail0' class="searchdetail">
<div>
<table class="searchgrid"
id="ctl00_ContentPanel_gvMaster_ctl02_gvDtails">
<tr>
<th>Status</th>
<tr class="searchrow">
<td>2</td>
</tr>
</table>
</div>
</div>
//主div
基础知识
//详细资料组
地位
2.
我无法让JQuery工作,时间也快用完了,所以现在我决定使用ajax控制工具包中的可折叠面板externder。当我有时间的时候,我会调查JQuery问题,谢谢你到目前为止提出的所有建议。如果有人还有更多,请告诉我。“当我单击某个项目时,它似乎甚至没有调用showhide。”如果您仅使用alert()函数简化该函数,它是否有效
代码在我看来很好。可能有一些id不匹配。您可以检查页面(使用此母版页的页面)中jquery的路径是否正确 为了确保如果调用showhide(),您可以在Firefox中使用Firebug并在函数中设置断点 此外,它还将为您提供更多的调试线索,并猜测出现了什么问题
代码对我来说似乎也没问题。您的javascript在src.endsWith(“plus.png”)处抛出了一个错误,因为js中没有内置的endsWith函数。将其替换为src.substr(-8)=“plus.png”,它可以工作:
<script type="text/javascript">
//master: id of div element that contains the information about master data
//details: id of div element wrapping the details grid
function showhide(master, detail) {
//First child of master div is the image
var src = $(master).children()[0].src;
//Switch image from (+) to (-) or vice versa.
if (src.substr(-8) == "plus.png")
src = src.replace('plus.png', 'minus.png');
else
src = src.replace('minus.png', 'plus.png');
//Set new image
$(master).children()[0].src = src;
//Toggle expand/collapse
$(detail).slideToggle("normal");
}
</script>
//master:包含主数据信息的div元素的id
//详细信息:包装详细信息网格的div元素的id
函数showhide(主、细节){
//主div的第一个子级是图像
var src=$(主).children()[0].src;
//将图像从(+)切换到(-),反之亦然。
如果(src.substr(-8)=“plus.png”)
src=src.replace('plus.png','minus.png');
其他的
src=src.replace('minus.png','plus.png');
//树立新形象
$(主).children()[0].src=src;
//切换展开/折叠
$(细节)。滑动切换(“正常”);
}
编辑-工作示例:
母版页。母版
<%@ Master Language="C#" AutoEventWireup="true" %>
<!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">
<title>Untitled Page</title>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"></script>
<script type="text/javascript">
//master: id of div element that contains the information about master data
//details: id of div element wrapping the details grid
function showhide(master, detail) {
//First child of master div is the image
var src = $(master).children()[0].src;
//Switch image from (+) to (-) or vice versa.
if (src.substr(-8) == "plus.png")
src = src.replace('plus.png', 'minus.png');
else
src = src.replace('minus.png', 'plus.png');
//Set new image
$(master).children()[0].src = src;
//Toggle expand/collapse
$(detail).slideToggle("normal");
}
</script>
<asp:ContentPlaceHolder id="head" runat="server">
</asp:ContentPlaceHolder>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:ContentPlaceHolder id="ContentPlaceHolder1" runat="server">
</asp:ContentPlaceHolder>
</div>
</form>
</body>
</html>
无标题页
//master:包含主数据信息的div元素的id
//详细信息:包装详细信息网格的div元素的id
函数showhide(主、细节){
//主div的第一个子级是图像
var src=$(主).children()[0].src;
//将图像从(+)切换到(-),反之亦然。
如果(src.substr(-8)=“plus.png”)
src=src.replace('plus.png','minus.png');
其他的
src=src.replace('minus.png','plus.png');
//树立新形象
$(主).children()[0].src=src;
//切换展开/折叠
$(细节)。滑动切换(“正常”);
}
Default2.aspx
<%@ Page Language="C#" MasterPageFile="~/MasterPage.master" AutoEventWireup="true" Title="Untitled Page" %>
<asp:Content ID="Content1" ContentPlaceHolderID="head" Runat="Server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
<div class="searchgroup" id='master0' onclick='showhide("#master0","#detail0")'>
<img id="ctl00_ContentPanel_gvMaster_ctl02_imgCollapsible" class="first" src="plus.png" style="border-width:0px;margin-right: 5px;" />
<span class="searchheader">ABC</span>
</div>
<div id='detail0' class="searchdetail">
<div>
<table class="searchgrid"
id="ctl00_ContentPanel_gvMaster_ctl02_gvDtails">
<tr>
<th>Status</th>
</tr>
<tr class="searchrow">
<td>2</td>
</tr>
</table>
</div>
</div>
</asp:Content>
基础知识
地位
2.
只是想一想,如果右键单击浏览器上的“查看源代码”,是否正确地看到jquery路径?我的经验告诉我,您需要解析母版页上的javascript URL(它适用于css,但不适用于js文件)
@Xaisoft不要尝试调试它,检查生成的html源代码并确保它进入showhide。在使用实际代码时,有些东西可以更快地识别。我的想法完全正确——查看呈现的html,如果您仍然被困在其中,请编辑您的帖子以包含html的相关部分;在showhide(master,detail)函数中,它可以正常工作。那么您的原始语句“当我单击某个项目时,它看起来甚至没有调用showhide”是不正确的。Jquery select语句可能无法选择正确的对象。在继续之前检查select对象的返回值。如何调试javascript,我不能设置断点。我在VS 2008工作,希望这会有所帮助。这来自著名的《ScottGu》圣经:-)在我看来,在VisualStudio中调试javascript是一件痛苦的事情。如果您有firefox和firebug扩展,就可以轻松调试javascript。这绝对是javascript调试的方法,除非你遇到一个只针对IE的bug……当我在没有母版页的页面上测试时,代码可以工作,但如果我把它放在母版页上,代码就不能工作。这条路是正确的。我可以直接将脚本放在页面本身而不是母版中吗?如果我不使用母版页,为什么不会出现此错误。当我将脚本放在母版页的头部分时,showhide函数不会启动,当我在firebug中设置断点时,它会说所有内容都未定义JQuery也可能有一个
<script src="<%= ResolveUrl("~") %>jquery-1.3.2.min.js" type="text/javascript"></script>