如何识别被点击的元素?

时间:2014-10-21 15:55:37

标签: javascript jquery

请帮助修复script

用户点击项目.menu_button后,有一个调用函数initForm()。此功能应打印一个

console.log(N)

但显示3:

console.log(1)
console.log(2)
console.log(3)

我不明白

JS:

$(document).ready(function() {
    // --- PLUGIN kalininModals INITIALIZATION ---
    $('.menu_button').kalininModals();
);

(function($){
    // --- options ---  
    $.fn.kalininModals = function(options) {                    
        var options = $.extend({},options);

        return this.each(function(e) {
            // --- properties ---   
            var self = $(this),
                selfModals = $('#modalOuter'),
                selfModalsWindow = $('#modalWindow'),
                head,
                info,
                actionsArr; 

            // --- methods ---  
            function initForm(formNum){
                //console.log('___' + self.text());

                if(formNum == 1){
                    console.log(1);

                    head = 'Обратный звонок';


                }
                else if(formNum == 2){
                    console.log(2);

                    head = 'Обратный звонок';


                }
                else if(formNum == 3){
                    console.log(3);

                    head = 'Обратный звонок';
                };          
            }

            function makeBody(){
                console.log('make');

                $('#head .h2').text(head);
                $('#info').html(info);
            }




            // --- handlers ---     
            function onClickControls(e){
                self = $(e.currentTarget);
                initForm(self.attr('data-form-num'));
                makeBody();

            }

            // --- events ---
            $('#menuButton1, #menuButton2, #menuButton3').on('click', onClickControls); 

        });
    };
})($);

1 个答案:

答案 0 :(得分:-1)

使用班级

<div class="menuButton">Menu 1</div>
<div class="menuButton">Menu 2</div>
<div class="menuButton">Menu 3</div>

和jquery

$(document).ready(function(){
    $(".menuButton").click(function(){
        alert($(this).html());
    });
});

希望它有所帮助!