请帮我解决这个问题。
我有一个与代码http://jsfiddle.net/LTYa2/14/的链接,它在jsfiddle中运行良好…但是如果我把同样的东西放在localhost中,有什么特别的原因吗?我还检查了javascripts和jquery,一切都是正确嵌入的(其他jsfiddle代码运行良好)……
有人能指导我如何放置这个代码吗?
$('q.clickMe1').click(function () {
// find first of following DIV siblings
// with class "textBox" and toggle it
$(this).nextAll('div.sri1:first').show();
$('div.sri1:visible').not($(this).nextAll('div.sri1:first')).toggle();
});html代码
<q class="clickMe1">Hire</q>
<br />
<div class="sri1"> - This text will be toggled</div>
<q class="clickMe1">Manage</q>
<br />
<div class="sri1"> - This text will be toggled 2</div>发布于 2012-08-14 16:56:42
默认情况下,jsFiddle将代码包装在onload中,这就是它工作的原因。因为我禁用了onload包装,所以Here在它不工作的地方是很麻烦的。
要让它为您工作,请将其包装为:
$(document).ready(function() {
$('q.clickMe1').click(function () {
// find first of following DIV siblings
// with class "textBox" and toggle it
$(this).nextAll('div.sri1:first').show();
$('div.sri1:visible').not($(this).nextAll('div.sri1:first')).toggle();
});
});假设您使用的是jQuery 1.7或更高版本,您可以避免这种包装,而使用.on(),如下所示:
$(document).on('click', 'q.clickMe1', function () {
//...your code here
});Updated fiddle -无负载,仍在工作。
https://stackoverflow.com/questions/11948868
复制相似问题