问题如下:
我在页面引用JQuery,实现某个功能如下:<a rel="example_group" href=1.jpg' title='1'>
<img src='1.jpg' alt='1' />
</a>
<a rel="example_group" href=2.jpg' title='2'>
<img src='2.jpg' alt='2' />
</a>
<a rel="example_group" href=3.jpg' title='3'>
<img src='3.jpg' alt='3' />
</a>当我标记为空色的那段是直接现在页面上的时候,JQuery执行没问题,如果我是用js动态生成的话,那JQuery代码就失效了,请问哪位大虾有知道为什么,有什么解决方案,谢谢
我在页面引用JQuery,实现某个功能如下:<a rel="example_group" href=1.jpg' title='1'>
<img src='1.jpg' alt='1' />
</a>
<a rel="example_group" href=2.jpg' title='2'>
<img src='2.jpg' alt='2' />
</a>
<a rel="example_group" href=3.jpg' title='3'>
<img src='3.jpg' alt='3' />
</a>当我标记为空色的那段是直接现在页面上的时候,JQuery执行没问题,如果我是用js动态生成的话,那JQuery代码就失效了,请问哪位大虾有知道为什么,有什么解决方案,谢谢
比如你原先有个class="a"的Button
你在jquery里$(".a").click()...
那么如果你在动态生成一个class='a'的Button的时候
就需要重新绑定他了
或者你可以用live来绑定~
引用的JQuery文件就不说了:在其中还有这么一段代码:$(document).ready(function ()
{
/*
* Examples - images
*/ $("a#example1").fancybox({
'titleShow': false
}); $("a#example2").fancybox({
'titleShow': false,
'transitionIn': 'elastic',
'transitionOut': 'elastic'
}); $("a#example3").fancybox({
'titleShow': false,
'transitionIn': 'none',
'transitionOut': 'none'
}); $("a#example4").fancybox(); $("a#example5").fancybox({
'titlePosition': 'inside'
}); $("a#example6").fancybox({
'titlePosition': 'over'
}); $("a[rel=example_group]").fancybox({
'transitionIn': 'none',
'transitionOut': 'none',
'titlePosition': 'over',
'titleFormat': function (title, currentArray, currentIndex, currentOpts)
{
return '<span id="fancybox-title-over">Image ' + (currentIndex + 1) + ' / ' + currentArray.length + (title.length ? ' ' + title : '') + '</span>';
}
}); /*
* Examples - various
*/ $("#various1").fancybox({
'titlePosition': 'inside',
'transitionIn': 'none',
'transitionOut': 'none'
}); $("#various2").fancybox(); $("#various3").fancybox({
'width': '75%',
'height': '75%',
'autoScale': false,
'transitionIn': 'none',
'transitionOut': 'none',
'type': 'iframe'
}); $("#various4").fancybox({
'padding': 0,
'autoScale': false,
'transitionIn': 'none',
'transitionOut': 'none'
});
});
只要你在加载完控件,然后在给他添加一些jquery得效果,我觉得应该是没有问题得
用这个来代替
$('.clickme').live('click', function() {
alert("Live handler called.");
});
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<title>公司新闻</title>
<link href="App_Themes/hemu/Base.css" rel="Stylesheet" type="text/css" media="all" />
<link href="App_Themes/hemu/font.css" rel="Stylesheet" type="text/css" media="all" />
<script type="text/javascript" src="JS/Ajax.js"></script>
<script type="text/javascript" src="JS/slider.js"></script>
<script type="text/javascript" src="JS/jquery-1.4.2.min.js"></script>
<script type="text/javascript" src="JS/jquery.mousewheel-3.0.2.pack.js"></script>
<script type="text/javascript" src="JS/jquery.fancybox-1.3.1.js"></script>
<script type="text/javascript" src="JS/pngobject.js"></script>
<script type="text/javascript" src="JS/jquery.livequery.js"></script>
<link rel="stylesheet" href="style/jquery.fancybox-1.3.1.css" type="text/css" />
<script type="text/javascript"><!--
var MyMar;
var Index = 0;
var ImgArray = new Array();
//获取信息
function CallBack()
{
if (XmlhttpReq.readyState == 4)
{
var NewsInfo = document.getElementById("Content");
var Div1 = document.getElementById("Div1");
var TheText = XmlhttpReq.responseText;
var TheInfo = TheText.split('||||||');
NewsInfo.innerHTML = TheInfo[0];
if (TheInfo[1] != "")
{
document.getElementById("LeftImageDiv").style.display = "";
document.getElementById("RightContent").style.width = 400 + "px";
Div1.innerHTML = TheInfo[1];
Move();
}
else
{
document.getElementById("LeftImageDiv").style.display = "None";
document.getElementById("RightContent").style.width = "98%";
}
}
}
//最终使用函数
function Result(Url)
{
var OutBox = document.getElementById("RightContent");
var ImgDiv = document.getElementById("Div1");
ImgDiv.innerHTML = "";
OutBox.scrollTop = 0;
document.getElementById("Div2").innerHTML = "";
ImgDiv.scrollTop = 0;
UseAjax("post", Url, true, CallBack);
clearInterval(MyMar);
}
function onLoad()
{
var alist = document.getElementById("List").getElementsByTagName("a");
Result("NewsInfo.aspx?ID=" + alist[0].id);
}
function Move()
{
var speed = 100;
var Div1 = document.getElementById("Div1");
var Div2 = document.getElementById("Div2");
var ImgDiv = document.getElementById("LeftImageDiv");
if (Div1.offsetHeight > ImgDiv.offsetHeight)
{
Div2.innerHTML = Div1.innerHTML;
}
Marquee();
MyMar = setInterval(Marquee, speed);
ImgDiv.onmouseover = function () { clearInterval(MyMar) }
ImgDiv.onmouseout = function () { MyMar = setInterval(Marquee, speed) }
} function Marquee()
{
var Div1 = document.getElementById("Div1");
var Div2 = document.getElementById("Div2");
var ImgDiv = document.getElementById("LeftImageDiv");
if (ImgDiv.scrollTop > Div1.scrollHeight)
{
ImgDiv.scrollTop = 0;
}
else
{
ImgDiv.scrollTop++;
}
}
function Up()
{
var OutBox = document.getElementById("RightContent");
var ContentDiv = document.getElementById("Content");
if (ContentDiv.offsetHeight > OutBox.offsetHeight)
{
OutBox.scrollTop -= 100;
}
}
function Down()
{
var OutBox = document.getElementById("RightContent");
var ContentDiv = document.getElementById("Content");
if (ContentDiv.offsetHeight > OutBox.offsetHeight)
{
OutBox.scrollTop += 100;
}
}
$(document).ready(function ()
{
/*
* Examples - images
*/ $("a#example1").fancybox({
'titleShow': false
}); $("a#example2").fancybox({
'titleShow': false,
'transitionIn': 'elastic',
'transitionOut': 'elastic'
}); $("a#example3").fancybox({
'titleShow': false,
'transitionIn': 'none',
'transitionOut': 'none'
}); $("a#example4").fancybox(); $("a#example5").fancybox({
'titlePosition': 'inside'
}); $("a#example6").fancybox({
'titlePosition': 'over'
}); $("a[rel=example_group]").fancybox({
'transitionIn': 'none',
'transitionOut': 'none',
'titlePosition': 'over',
'titleFormat': function (title, currentArray, currentIndex, currentOpts)
{
return '<span id="fancybox-title-over">Image ' + (currentIndex + 1) + ' / ' + currentArray.length + (title.length ? ' ' + title : '') + '</span>';
}
}); /*
* Examples - various
*/ $("#various1").fancybox({
'titlePosition': 'inside',
'transitionIn': 'none',
'transitionOut': 'none'
}); $("#various2").fancybox(); $("#various3").fancybox({
'width': '75%',
'height': '75%',
'autoScale': false,
'transitionIn': 'none',
'transitionOut': 'none',
'type': 'iframe'
}); $("#various4").fancybox({
'padding': 0,
'autoScale': false,
'transitionIn': 'none',
'transitionOut': 'none'
});
});
// --></script>
</head>
<body onload="onLoad()">
<form id="form1" runat="server">
<div class="wrapper">
<img src="banner.gif" />
</div>
<div class="content_wrap" style="display: inline-table">
<h1>
<img src="Images/h3title/gongsixinwen.jpg" alt="" /></h1>
<div class="news" style="padding-top: 10px; width:766px">
<div class="newslist">
<ul id="List" style="height: 265px">
<asp:Repeater ID="rp_List" runat="server">
<ItemTemplate>
<li><a id='<%# Eval("ID") %>' href="javascript:Result('NewsInfo.aspx?ID=<%# Eval("ID") %>')">
<%# Eval("Title") %></a></li>
</ItemTemplate>
</asp:Repeater>
</ul>
<div class="nc_page">
<div style="float: right; margin-right: 10px; display: inline">
<asp:Literal ID="l_Pager" runat="server"></asp:Literal>
</div>
</div>
</div>
<div class="newscon" style="width:500px">
<div id="HiddenDiv" class="nc_con" style="height: 245px; margin: 0 auto; text-align: center;
width: 500px">
<div id="LeftImageDiv" style="width: 90px; height: 243px; border: solid 1px #777777">
<div id="Div1">
</div>
<div id="Div2">
</div>
</div>
<div id="RightContent" style="width: 400px; height: 245px; padding-right:5px">
<div id="Content" style="width:100%">
</div>
</div>
</div>
<div class="nc_page">
<a href="javascript:Up()">PREV</a> <a href="javascript:Down()">NEXT </a>
</div>
</div>
</div>
</div>
<div style="clear: both; margin-top: 54px;">
<!--#Include File="Inc/footer.aspx"-->
</div>
</div>
</form>
</body>
</html>
其中CallBack()
自己写的一个ajax回调函数,意思就是返回这样:
<ul>
<li>
<a href='1.gif' ref=''>
<img src='1.gif' />
</a>
</li>
<ul>的东西,然后innerHTML在一个ID为DIV1的div中,但是这样,点击图片,始终是在一个新窗口打开,而不是弹出生成缩略图的层。。请大家给看看
$("a[rel=example_group]").fancybox({
'transitionIn': 'none',
'transitionOut': 'none',
'titlePosition': 'over',
'titleFormat': function (title, currentArray, currentIndex, currentOpts)
{
return '<span id="fancybox-title-over">Image ' + (currentIndex + 1) + ' / ' + currentArray.length + (title.length ? ' ' + title : '') + '</span>';
}
});
<ul>
<li>
<a href='1.gif' ref=''>
<img src='1.gif' />
</a>
</li>
</ul>
如果,这是直接写在页面上的,而不是去动态生成的,他就没问题,而生成的,点击图片后,他直接在新窗口中打开了。。
哎,杯具
function CallBack()
{
if (XmlhttpReq.readyState == 4)
{
var NewsInfo = document.getElementById("Content");
var Div1 = document.getElementById("Div1");
var TheText = XmlhttpReq.responseText;
var TheInfo = TheText.split('||||||');
NewsInfo.innerHTML = TheInfo[0];
if (TheInfo[1] != "")
{
document.getElementById("LeftImageDiv").style.display = "";
document.getElementById("RightContent").style.width = 400 + "px";
Div1.innerHTML = TheInfo[1];
Move();
}
else
{
document.getElementById("LeftImageDiv").style.display = "None";
document.getElementById("RightContent").style.width = "98%";
}
$("a[rel=example_group]").fancybox({
'transitionIn': 'none',
'transitionOut': 'none',
'titlePosition': 'over',
'titleFormat': function (title, currentArray, currentIndex, currentOpts)
{
return '<span id="fancybox-title-over">Image ' + (currentIndex + 1) + ' / ' + currentArray.length + (title.length ? ' ' + title : '') + '</span>';
}
}); }
}
可以改成:
<a href='http://localhost/项目/image/1.gif' ref=''>
就是你的图片的详细路径.
我一般没有什么IM可以用