0

请帮我解决这个问题...

我有一个与代码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>
4

1 回答 1

2

jsFiddleonload默认情况下将代码包装在其中,这就是它起作用的原因。是小提琴,因为我禁用了 onload 包装,所以它不起作用。

为了让它适合你,像这样包装它:

$(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
});

更新了小提琴- 没有 onload 并且仍在工作。

于 2012-08-14T08:56:41.500 回答