代码在jsfiddle中工作,但在本地服务器中不起作用


Code is working in jsfiddle ,but not in localserver

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

我有一个代码链接 http://jsfiddle.net/LTYa2/14/它在 JSFIDDLE 中工作正常......但是如果我在 localhost 中放置同样的东西,它不起作用任何特定原因?我还检查了 JavaScript 和 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();
});

网页代码

<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>

jsFiddle 默认将代码包装在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
});

更新的小提琴 - 没有加载并且仍在工作。