jQuery 的导航问题。链接仅在一个 div 中有效
我正在尝试创建一个仅使用一个带有 3x3 div 数组的大页面的网站,其中用户一次只能看到一个 div,并且 jQuery 将使用动画链接来引导屏幕周围所需的 div。 我从 这个免费代码页 中得到了这个想法,但我希望链接位于每个 div 内部而不是周围外面有自己的容器。
现在,我的链接在“home”div(中央的)上工作正常,动画全部正常工作,但是当我将 html 中的链接代码复制+粘贴到另一个 div 时,它们拒绝工作。我不知道为什么,因为所有的 div 都是相同的,并且代码似乎与引用中心 div 无关。
html 代码的内容与此类似(只有 9 个链接):
<div id="one" class="elements">
<div class="block25">
<span class="go1"><img src="images/go1.png" alt="One"><h4>ONE</h4></span>
</div>
</div>
而 jQuery 是这样的:
$('.go1').click(function(){
$('#one').click();
});
$('#one').click(function(){;
currentId = $('selected').attr('id');
goId = section[0][0];
$target = $('div[id=' + goId +']');
$paneTarget.stop().scrollTo($target, 800, { margin: true } );
$('div[id=' + currentId +'], div[id=' + goId +']').toggleClass('selected');
});
我做错了什么?任何帮助将不胜感激。
I am trying to make a website that only uses one large page with a 3x3 array of divs where the user will only see one div at a time and the jQuery will direct the screen around using links to animate to the required div.
I got the idea from this free code page but I would like the links to be inside each div instead of around the outside within their own container.
Right now I have the links working fine on the 'home' div (the central one) with animations all working correctly but when I copy + paste the link code in the html to another div, they refuse to work. I have no idea why because all the divs are identical and the code seemingly has nothing to do with referencing the central div.
The html code goes something along the lines of this (only there are 9 links):
<div id="one" class="elements">
<div class="block25">
<span class="go1"><img src="images/go1.png" alt="One"><h4>ONE</h4></span>
</div>
</div>
While the jQuery is as thus:
$('.go1').click(function(){
$('#one').click();
});
$('#one').click(function(){;
currentId = $('selected').attr('id');
goId = section[0][0];
$target = $('div[id=' + goId +']');
$paneTarget.stop().scrollTo($target, 800, { margin: true } );
$('div[id=' + currentId +'], div[id=' + goId +']').toggleClass('selected');
});
What am I doing wrong? Any help would be greatly appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为这是因为您的脚本引用了
div
的ID
。ID
对于页面来说应该是唯一的。因此,该脚本只能使用一个div
,因为 a) 它被编写为仅访问一个div
,b) 因为ID
是唯一的。要解决此问题,您可以执行以下两件事:
为每个
div
提供唯一的ID
并根据需要多次复制脚本(如果您不确定的话,这是最简单的方法)代码)。给
div
相同的class
并修改代码以适用于每个类实例。I think it is because your script is referencing the
div
sID
.ID
s are supposed to be unique to the page. So the script would only work with onediv
because a) it is written to access only onediv
and b) becauseID
s are unique.To fix this, you could do two things:
give each
div
a uniqueID
and replicate the script as many times as needed (easiest if you are not sure of the code).give the
div
s the sameclass
and revise the code to be applicable to each class instance.