无法将click / mouseup / mousedown事件侦听器添加到<a href=""></a>

时间:2011-11-07 19:06:42

标签: javascript jquery click mousedown mouseup

我正在尝试使用HTML + CSS + Javascript + jQuery在<a href="#" ...>标记上创建一个按钮。按钮显示正常。但是,当我点击它时,除了默认行为(即,导航到“#”)之外没有任何事情发生。对我做错了什么的建议?这是我的代码:

    //container is a div created with 
    //borderDiv = document.createElement('div') and 
    //document.body.appendChild(borderDiv);

function addMenuNavigation(container) {
    var temp_innerHTML = '' +  
    '<div id="titleBar" class="titleBar">' +

    '<div id="menuTitle" class="menuTitle"><img id="titleImage" style="height: 70px; position: absolute; top: 2px; left:120px"></img></div>' +

    '<a href="#" class="leftButton" id="leftButton">' + 
    '   <div style="position: relative; top: 6px;"><img src="' + getURL('img/menu/iPhoneStyle/chevronLeft.png') + '"></img></div>' +
    '</a>' +

    '</div>' + //titleBar
    '';

    container.innerHTML = temp_innerHTML;

    var $leftButton = $('#leftButton');
    console.dir($leftButton); //Indeed it does display the #leftButton element


    //FIXME BUG 'MenuNavigation' below functions do not get registered ...

    $('#leftButton').mousedown(function(e) {
        $('#leftButton').addClass("pressed");
        console.log('leftButton down ' + this.id);
    });
    $('#leftButton').mouseup(function(e) {
        console.log('leftButton up ' + this.id);
        $(this).removeClass("pressed");
    });
    $('#leftButton').click(function(e){
        console.log('leftButton clicked ' + this.id);
        click_e.preventDefault();
    });

}

.css如下

.titleBar {
    ...
    display: block;
    height: 63px;
    opacity: 0.8;
    padding: 6px 0;
    ...           
    background-image: -webkit-gradient(linear, left top, left bottom, 
                                       from(#bbb), to(#444));                                                                    


    z-index: 35;                       
}


.leftButton:not(ac_hidden), .leftButton:not(pressed) {
    left: 6px;
    ...
    -webkit-border-image: url(../img/menu/iPhoneStyle/back_button.png) 0 8 0 8;
}


.leftButton.pressed{
    -webkit-border-image: url(../img/menu/iPhoneStyle/back_button_clicked.png) 0 8 0 8;
}

我正在使用Chrome。

有什么建议吗?谢谢。

3 个答案:

答案 0 :(得分:2)

$('#leftButton').click(function(e){
        console.log('leftButton clicked ' + this.id);
        click_e.preventDefault();
    });

应该是e.preventDefault();

答案 1 :(得分:0)

在您尝试将事件附加到DOM之前,浏览器是否还没有完成将HTML添加到DOM中?

即,在这两行之间?

container.innerHTML = temp_innerHTML;

var $leftButton = $('#leftButton');

在这种情况下,您可能希望使用.live(),http://api.jquery.com/live/

附加事件

答案 2 :(得分:0)

我个人更喜欢以这种方式添加我的事件处理程序:

function addMenuNavigation(container) {
    var container = $(container).empty();
    var titleBar = $('<div id="titleBar/>');
    var leftButton = $('<a href="#">Image here</a>').mousedown(leftButtonMousedown).mouseup(leftButtonMouseup).click(leftButtonClick).appendTo(titleBar);
    titleBar.appendTo(container);
}

function leftButtonMouseDown(e) { //mouse down handler
}
function leftButtonMouseUp(e) { //mouse up handler
}
function leftButtonClick(e) {
    e.preventDefault();
    // handle the click here
}
相关问题