2

jspに以下のアンカータグがあります

<a href="javascript:submit(this)">save</a>
<a href="javascript:alert(this)">something</a>

デフォルトの href 動作を停止し、onclick でこれらの関数を呼び出す必要があります。同じために、私はこのコードを書きました:

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

$(document).ready(function(){
      $("a[href^='javascript']").each(function(){
            alert("replacing");
            var w=$(this).attr('href');

            $(this).attr('href','#');

            $(this).attr("onclick",w);
   });
});

ただし、リンクをクリックしても関数は呼び出されません。どこが間違っているのか教えてください。IE8を使用しています。

4

2 に答える 2

3

これを試して:

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

$(document).ready(function(){    
      $("a[href^='javascript']").attr('href','#');    
});
于 2012-11-26T21:28:29.090 に答える
-1

$(document).ready(function() {
  var mydiv = document.getElementById("myDiv");
  var aTag = document.createElement('a');
  aTag.setAttribute('href', "#");
  aTag.setAttribute('id', "tagId");
  aTag.innerHTML = "link text";

  mydiv.appendChild(aTag);

  $("#tagId").on('click', function(e) {
    alert(1)

  });
});
   

于 2017-10-31T07:40:08.507 に答える