0

我只有一个简单的超链接,当我尝试添加click()事件处理程序时它不起作用。我试过了,.on(), .live(), .delegate()他们都没有工作。我该怎么做?

我不能做一个小提琴,因为我不知道为什么我的 bootstrap.min.css 没有被包含在外部资源部分。

请告诉我任何其他方法。

我的代码:

HTML:

<div class="navbar nav-fixed-top navbar-inverse" style="min-height: 50px; margin-bottom: 0; box-shadow: 0px 2px 5px #222;">
    <!-- Creating a fixed to the top navbar with no bottom margin and a nice little shadow and increasing the height -->
    <div class="navbar-inner" style="border-radius: 0;min-height: 50px; ">
        <!-- Creating the inner content of navbar and increasng it's height aswell to match the parent's height aswell -->
        <div class="container-fluid">
            <!-- Main header starts -->
            <button type="button" class="btn btn-navbar" data-toggle="collapse" data-target=".nav-collapse">
                <!-- Small screen collapseable menu starts -->  <span class="icon-bar"></span>  <span class="icon-bar"></span>  <span class="icon-bar"></span> 
                <!-- The collapseable menu icon(becuase the three `icon-bar` make an icon :P) -->
            </button>   <a class="brand" href="#"><img src="img/logo.png"
                    style="height: 25px;" /> </a> 
            <!-- adding the logo to the header -->
            <div class="nav-collapse collapse pull-right">
                <!-- div holding collapseable menu's data -->
                <ul class="nav">
                    <!-- creating a list to be created in the collapseable menu -->
                    <li><a href="#" id="register_button">Register</a>
                    </li>
                    <!-- adding an hyper link to the collapseable menu -->
                </ul>
                <!-- closing the list -->
            </div>
            <!-- closing the div holding collapseable menu's data -->
        </div>
        <!-- closing the main header -->
    </div>
    <!-- closing inner content holder of navbar div -->
</div>
<!-- closing the fixed to top navbar holder -->

CSS: 使用 bootstrap.min.css

JS:

var registerBtn = $("#register_button"); //storing the hyperlink of register in a variable
var i = 0; //click count
$(registerBtn).on('click', function (e) {
    i++;
    if (i % 2 === 0) {
        $("#register_frm").fadeToggle('fast', function () {
            $("#login_frm").fadeToggle('fast');
        });
        $("#register").text('Register');
    } else {
        $("#login_frm").fadeToggle('fast', function () {
            $("#register_frm").fadeToggle('fast');
        });
        $("#register").text('Login');
    }
});

有什么建议吗?

4

2 回答 2

1

更改您的代码:

$(function() {
    var registerBtn = $("#register_button"); //storing the hyperlink of reg....

    var i = 0; //click count

    registerBtn.on('click', function (e) { // since registerBtn is a reference to anchor tag
        e.prevenDefault(); // prevent default behaviour of anchor tag
        .........
    });
});
于 2013-09-04T17:10:33.063 回答
1

它可能在 DOM 中创建元素之前执行处理程序绑定。将代码移动到文档的末尾(在创建元素之后),或者将其包装在一个就绪事件中:

$(function() {
    var registerBtn = $("#register_button"); //storing the hyperlink of register in a variable
    var i = 0; //click count
    $(registerBtn).on('click', function (e) {
        i++;
        if (i % 2 === 0) {
            $("#register_frm").fadeToggle('fast', function () {
                $("#login_frm").fadeToggle('fast');
            });

    $("#register").text('Register');
        } else {
            $("#login_frm").fadeToggle('fast', function () {
                $("#register_frm").fadeToggle('fast');
            });
            $("#register").text('Login');
        }
    });
});
于 2013-09-04T16:21:37.387 回答