Kin*_*ard 24 javascript jquery
我正在构建一个可通过将脚本拖放到预设目录中而扩展的Web应用程序.脚本需要在转到页面之前读取文件头,因此我需要在单击链接时使用.preventDefault()并首先运行一些JScript.不幸的是我无法通过.preventDefault()
这是我的链接格式:
<a href="path/to/file.php" class="filelink">Some Text</a>
Run Code Online (Sandbox Code Playgroud)
这是我试图用来停止默认操作的jquery:
$( '.filelink' ).click( function( e ) {
e.preventDefault();
//do some other stuff here
});
Run Code Online (Sandbox Code Playgroud)
编辑:
更多信息:
这<script>...</script>是我的页脚.此函数在INSIDE $(document).readyAFTER之后$.ajax构建此函数试图侦听点击的链接列表.
r3w*_*3wt 71
由于您的链接会动态附加到页面,因此您需要使用document.on()捕获点击事件.
将事件侦听器附加到动态内容的语法如下
$(document).on( event, selector, callback );
所以你的点击处理程序将成为:
$(document).on('click','.filelink',function(e){
e.preventDefault();
//code here
return false;
});
Run Code Online (Sandbox Code Playgroud)
我认为您错过了$(document).ready()
请按以下方式更新您的代码:
$(document).ready(function(){
$( '.filelink' ).click( function( e ) {
e.preventDefault();
//do some other stuff here
});
})
Run Code Online (Sandbox Code Playgroud)