内联 Javascript 导致 jQuery Cycle 插件停止
我是 jquery 和 Javascript 的新手,我遇到了一个问题,我希望这里有人能够解决。我已经设置了一个 jquery Cycle 插件来显示 div 中的一些内容。现在我也有某些链接在另一个 div 中加载一些不同的内容。加载后,循环工作正常,但是当我单击任何链接并加载另一个 div 中的内容时,该循环停止工作。请注意,我在链接的 href 中使用了内联 javascript。这就是造成冲突的原因。我的代码:
<script type="text/javascript">
$(document).ready(function() {
$(".paras").cycle({
fx: 'fade',
speed: 200,
timeout: 1000,
next: '.tnext',
prev: '.tprev',
cleartype: '1'
})
content(1); /* To load content 1 on page load */
});
function content(i){
if (i == 1) {/* Code to load content in BIG DIV from external HTML */}
if (i == 2) {/* Code to load content in BIG DIV from external HTML */}
if (i == 3) {/* Code to load content in BIG DIV from external HTML */}
}
</script>
<ul>
<li><a class="home" href="javascript:content(1)">Home</a></li>
<li><a class="work" href="javascript:content(2)">Work</a></li>
<li><a class="about" href="javascript:content(3)">About</a></li>
</ul>
<div>
/* This is the big div */
</div>
<div class="paras">
/* This is the small div. Content loaded using jquery cycle. Stops cycling when content loaded in BIG DIV */
</div>
任何人都可以建议如何保持 Cycle 工作,同时保持内联 javascript 完整吗?或者你有更好的建议吗?
I am new to jquery and Javascript and I am running into a problem which I hope somebody here will be able to solve. I have setup a jquery Cycle plugin to display some content in a div. Now I also have certain links load some different content in another div. Upon load, the cycle works fine, but when I click any of the links and load the content in the other div, this cycle stops working. Please do note that I am using an inline javascript in the hrefs of the links. And that is creating the conflict. My code:
<script type="text/javascript">
$(document).ready(function() {
$(".paras").cycle({
fx: 'fade',
speed: 200,
timeout: 1000,
next: '.tnext',
prev: '.tprev',
cleartype: '1'
})
content(1); /* To load content 1 on page load */
});
function content(i){
if (i == 1) {/* Code to load content in BIG DIV from external HTML */}
if (i == 2) {/* Code to load content in BIG DIV from external HTML */}
if (i == 3) {/* Code to load content in BIG DIV from external HTML */}
}
</script>
<ul>
<li><a class="home" href="javascript:content(1)">Home</a></li>
<li><a class="work" href="javascript:content(2)">Work</a></li>
<li><a class="about" href="javascript:content(3)">About</a></li>
</ul>
<div>
/* This is the big div */
</div>
<div class="paras">
/* This is the small div. Content loaded using jquery cycle. Stops cycling when content loaded in BIG DIV */
</div>
Can anyone suggest how to keep Cycle working while also keeping the inline javascript intact? Or do you suggest something better.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果不了解有关 Cycle 插件的更多信息,则不确定,但尝试将三个 href 更改为“#”,然后将内联操作移至 onclick,如下所示:
更好的是终止 onclick 并动态应用侦听器;你已经有了唯一的类名,所以你不需要 1,2,3。尽管这与您的问题无关,但具体做法如下:
我还将 classNames 更新为 ID。这不是必需的,但无论如何,您实际上都在有效地使用类作为唯一标识符,这就是 ID 的用途;选择使用它也更有效。
最后,我建议您还考虑将“#”替换为非 JavaScript 访问者(尤其是搜索引擎)的 URL。
Not sure without knowing more about the Cycle plugin, but try changing the three hrefs to "#", then move the inline action to an onclick, like so:
Even better would be to kill the onclicks and apply the listener dynamically; you already have unique classnames, so you don't need the 1,2,3. Even though that is not relevant to your problem, here is how to do it that way:
I also updated the classNames to IDs. This is not necessary, but you are effectively using classes as unique identifiers anyway, and that is what an ID is for; it is also more efficient to select using it.
Finally, I suggest you also consider replacing the "#" with URLs for non-JavaScript visitors (esp. search engines).