简体   繁体   English

这个jquery脚本有什么问题吗?

[英]whats wrong with this jquery script?

so im trying to apply a basic on click show hide element but for some reason it doesnt take no effect 所以即时通讯尝试应用基本的点击显示隐藏元素,但由于某种原因它不起作用

im using it via an external javascript file and including the latest jquery library both included in my master page firebug shows the code so i know its picking it up 即时通过外部javascript文件使用它,并包括我的母版页中包含的最新jquery库firebug显示代码,所以我知道它捡起它

heres the code ive tried 继承了我试过的代码

$(document).ready(function () {
// hides the divx as soon as the DOM is ready
$('#ecom').hide();
// shows the div on clicking the noted link  
$('.eco').click(function () {
    $('#ecom').show('slow');
    return false;
});
// hides the div on clicking the noted link  
$('.eco').click(function () {
    $('#ecom').hide('fast');
    return false;
}); 
});

html HTML

<h2 class="eco">Ecommerce Web Design</h2>
<div id="ecom">content</div>

I dont see the problem myself 我自己也没有看到这个问题

This was the solution i used in the end does what i want it to do :) Thankyou all for the answers too 这是我最终使用的解决方案做我想要它做的:)谢谢你们所有的答案

$(document).ready(function () {
$('#ecom').hide();
$('.eco').click(function () {
    $('#ecom').toggle('slow');
    return false;
});

Both handlers will fire at the same time. 两个处理程序将同时触发。

I think you're looking for .toggle() : 我想你正在寻找.toggle()

Example: http://jsfiddle.net/patrick_dw/uuJPc/ 示例: http //jsfiddle.net/patrick_dw/uuJPc/

$('.eco').toggle(function () {
    $('#ecom').show('slow');
    return false;
},function () {
    $('#ecom').hide('fast');
    return false;
});

Now it will alternate between clicks. 现在它将在点击之间交替。


EDIT: If you were to use .toggle() in the manner that @Tgr mentioned below, you would need some way to distinguish between the "slow"/"fast" you have in your code. 编辑:如果你以.toggle()的方式使用.toggle() ,你需要一些方法来区分代码中的"slow"/"fast"

Here's one way: 这是一种方式:

Example: http://jsfiddle.net/patrick_dw/uuJPc/1/ 示例: http //jsfiddle.net/patrick_dw/uuJPc/1/

$('#ecom').hide();
$('.eco').click(function () {
    var i = 0;
    return function() {
        $('#ecom').toggle(i++ % 2 ? 'slow' : 'fast');
        return false;
    };
}());

or like this: 或者像这样:

Example: http://jsfiddle.net/patrick_dw/uuJPc/2/ 示例: http //jsfiddle.net/patrick_dw/uuJPc/2/

$('#ecom').hide();
$('.eco').click(function () {
    var ec = $('#ecom');
    ec.toggle(ec.is(':visible') ? 'slow' : 'fast');
    return false;
});

You can use toggle instead of doing the whole thing manually, but that's sort a shortcut. 您可以使用toggle而不是手动完成整个操作,但这是一种快捷方式。 If you're trying to learn this stuff, I recommend creating it from scratch, and only using shortcuts when you feel like you fully understand what's going on behind the scenes. 如果您正在尝试学习这些东西,我建议您从头开始创建它,并且只有在您完全理解幕后发生的事情时才使用快捷方式。

$(document).ready(function(){
    // Hide ``#ecom`` on page load.
    $('#ecom').hide();
    // Attach only one click handler (yours was attaching a handler that showed
    // the div and then on that hid the div, so it was always shown, and then
    // hidden instantly, therefore canceling your intended effect.
    $('.eco').click(function(e){
        // ``preventDefault()`` is the W3 correct way to stop event propagation
        // and works cross-browser. It's a bound method on all DOM events.
        // Notice I've given my click handler function an argument ``e``. When
        // you register an event handler function, the event that causes the
        // handler function to be called is provided as the first argument handler
        // function, thus giving us ``e`` as a local reference to the event.
        e.preventDefault();
        // Use ``is()`` to compare the element against the ``:hidden`` selector.
        // This will tell us if it's hidden or not.
        if($('#ecom').is(':hidden'))
        {
            $('#ecom').show('slow');
        } else {
            $('#ecom').hide('fast');
        }
    });
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM