Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/kotlin/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 拦截超链接点击_Javascript - Fatal编程技术网

Javascript 拦截超链接点击

Javascript 拦截超链接点击,javascript,Javascript,我正在尝试拦截此链接上的点击: <a id="test-button" href="#">Test</a> 有了这个js: (function() { $('#test-button').click(function (event) { console.log("This code never gets called.") event.preventDefault(); $('#alert-placeholde

我正在尝试拦截此链接上的点击:

<a id="test-button" href="#">Test</a>

有了这个js:

(function() {
    $('#test-button').click(function (event) {
        console.log("This code never gets called.")
        event.preventDefault();
        $('#alert-placeholder').html(['<div class="alert"><a class="close"',
                                      'data-dismiss="alert">×</a>',
                                      '<span>"+message+"</span></div>'].join())
        return false;
    })
    console.log("yes, this code loads");
    debugger;
})();
(函数(){
$(“#测试按钮”)。单击(函数(事件){
log(“此代码永远不会被调用。”)
event.preventDefault();
$('#警报占位符').html(['×',
“+message+”].join())
返回false;
})
log(“是的,此代码加载”);
调试器;
})();
但是加载URL
“#”
,并且click()函数中的代码不运行。我错过了什么


我正在使用引导的flask应用程序中使用此代码。

似乎您正在尝试将事件处理程序附加到尚不存在的元素

(function() {
})();
仅创建本地作用域,但不保证加载DOM。要确认它,请添加
console.log($('#测试按钮').length)到您的代码

您需要的是用

$(function() {
    // your code is here
});
反而