Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/408.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 动态地将onclick事件添加到锚定标记,不起作用_Javascript_Jquery - Fatal编程技术网

Javascript 动态地将onclick事件添加到锚定标记,不起作用

Javascript 动态地将onclick事件添加到锚定标记,不起作用,javascript,jquery,Javascript,Jquery,我的jsp中有下面的锚标记 <a href="javascript:submit(this)">save</a> <a href="javascript:alert(this)">something</a> 但是,单击链接时不会调用这些函数。你能告诉我哪里出了问题吗。我正在使用IE 8。试试这个: $(document).on("click", "a[href^=\'#\']",function (e) { alert("I am bein

我的jsp中有下面的锚标记

<a href="javascript:submit(this)">save</a>
<a href="javascript:alert(this)">something</a>
但是,单击链接时不会调用这些函数。你能告诉我哪里出了问题吗。我正在使用IE 8。

试试这个:

$(document).on("click", "a[href^=\'#\']",function (e) {
   alert("I am being called #");
   e.preventDefault();
   return false;
});

$(document).ready(function(){    
      $("a[href^='javascript']").attr('href','#');    
});
$(文档).ready(函数(){
var mydiv=document.getElementById(“mydiv”);
var aTag=document.createElement('a');
aTag.setAttribute('href',“#”);
aTag.setAttribute('id','tagId');
aTag.innerHTML=“链接文本”;
mydiv.appendChild(aTag);
$(“#tagId”)。在('click',函数(e){
警报(1)
});
});

“我哪里出错了?我正在使用IE 8。”-那里。@H2CO3哈哈。--您是否尝试过“onclick=function()”?为什么不在初始标记中使用href=“#”和onclick=“submit(this)”呢?“onclick”不需要/想要像href那样的“javascript:”前缀…我想这是您的问题的根源。看起来您正试图在带有
href=“#“”的锚存在之前附加单击处理程序。另外,
.live()`也不推荐使用。改用
.on()
。@ryan。。我无法更改初始标记,因为在我的代码中很多地方已经存在初始标记,手动更新需要很多时间
$(document).on("click", "a[href^=\'#\']",function (e) {
   alert("I am being called #");
   e.preventDefault();
   return false;
});

$(document).ready(function(){    
      $("a[href^='javascript']").attr('href','#');    
});