href属性中的JavaScript函数调用

JavaScript function call in href attribute

本文关键字:JavaScript 函数调用 属性 href      更新时间:2023-09-26

我正试图通过<a>标记中的href属性调用一个基本的javascript函数。我以前已经做过无数次了,但现在每个浏览器都在jQuery中对hrefjavascript调用抛出异常,有些浏览器选择不运行它,而是打开一个空白的新窗口。在这一点上,我只是被打晕了。有什么想法吗?

HTML:

<td>
    <a href="javascript:showQuote(12);" data-toggle='modal' target='#quoteBox'>
        View Quote
    </a>
</td>

JavaScript/jQuery:

function showQuote(id){
    $("#spot").load("viewQuote.php?id=" + id);
    $('#modal').modal('show');
}

Chrome是唯一一个可以使用它的浏览器(它仍然会抛出一个错误)。我已经通过一些在线验证器运行了这一点,结果它们是干净的。

此外,我使用的是jQuery 1.11.3版本。

最好用"jquery"样式编写。

更新的答案-使用当前控件id

<td>
    <a href="" data-toggle='modal' id="12" class="mylink" target='#quoteBox'>
        View Quote
    </a>
</td>

在Document.Ready.中

 $('.mylink').on('click',function(event){
      event.preventDefault();
      var id = $(this).attr('id');
      $("#spot").load("viewQuote.php?id=" + id);
      $('#modal').modal('show');
 })

HTML

 <td><a href="javascript:showQuote(12,event);" data-toggle='modal' target='#quoteBox'>View Quote</a></td>

Javascript

function showQuote(id,event){
$("#spot").load("viewQuote.php?id=" + id);
$('#modal').modal('show');
 event.preventDefault();
 return false;
}