使用jquery捕获链接中的单击

使用jquery捕获链接中的单击,jquery,Jquery,在我的项目中,我有以下页面,这是一个带有指向我的应用程序所有部分的链接的仪表板: <div class="navbar navbar-inverse navbar-fixed-top" role="navigation"> <div class="container"> <div class="navbar-header"> <button type="button" class="navbar-t

在我的项目中,我有以下页面,这是一个带有指向我的应用程序所有部分的链接的仪表板:

    <div class="navbar navbar-inverse navbar-fixed-top" role="navigation">
      <div class="container">
        <div class="navbar-header">
          <button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".navbar-collapse">
            <span class="sr-only">Toggle navigation</span>
            <span class="icon-bar"></span>
            <span class="icon-bar"></span>
            <span class="icon-bar"></span>
          </button>
          <a class="navbar-brand" href="#">LigaDesportiva</a>
        </div>
        <div class="collapse navbar-collapse">
          <ul class="nav navbar-nav">
            <li><a href="clube.html">Clubes</a></li>
            <li><a href="dirigente.html">Dirigentes</a></li>
            <li><a href="jogadore.html">Jogadores</a></li>
            <li><a href="liga.html">Ligas</a></li>
            <li><a href="usuario.html">Usu&aacute;rios</a></li>
          </ul>
            <ul class="nav navbar-nav navbar-right">
                <li class="dropdown"> <a class="dropdown-toggle" data-toggle="dropdown">${usuario.nome} <b class="caret"></b></a>
                    <ul class="dropdown-menu">
                        <li><a href="usuario_perfil.html">Perfil</a></li>
                        <li><a href="usuario_historico.html">Hist&oacute;rico</a></li>
                        <li><a href="logout.html">Sair</a></li>
                    </ul>
                </li>
            </ul>
        </div><!--/.nav-collapse -->
      </div>
    </div>

    <div class="container">
        <div id="data"></div>    
    </div><!-- /.container -->

页面末尾的jquery代码应该捕获“click”事件,并显示一个警报(现在仅用于测试)和一个BootstrapDialog,但这正在发生。我做错了什么?

将页面底部的JS部分更改为:

$('a').click(function(event){
        event.preventDefault();
        $.get($('a').attr('href'), function(destino){
            alert('destino = '+destino);
            BootstrapDialog.show({
                    title: 'Draggable Dialog',
                    message: 'destino = '+destino+' .Try to drag on dialog title to move your dialog.',
                    draggable: true
            });
        });

    });
如果这也不起作用,也许您应该等到DOM就绪被触发。但不确定这一点,因为JS代码位于页面底部,所以在执行脚本块时应该加载HTML


您还需要
event.preventDefault()
取消链接默认的单击操作。

将处理程序绑定到单击事件的代码,
$('a')。在解析页面之前,可能会调用click(function(){
,在这种情况下,选择器
$('a')
不会获取锚元素

另外,在处理程序中,您正在对文档中的所有链接执行$.get操作。我假设您只需要单击的链接,您可以使用
$(this)
获得该链接

最后,您需要防止元素上单击事件的默认行为

请尝试以下方法:

<script>
    // The ready event handler is called after the page is ready.
    $().ready(function () {
        $('a').click(function(e) {
            e.preventDefault();

            $.get($(this).attr('href'), function(destino) {
                alert('destino = '+destino);
            });
            BootstrapDialog.show({
                title: 'Draggable Dialog',
                message: 'destino = '+destino+' .Try to drag on dialog title to move your dialog.',
                draggable: true
            });
        });
    });
</script>

//就绪事件处理程序在页面就绪后调用。
$().ready(函数(){
$('a')。单击(函数(e){
e、 预防默认值();
$.get($(this.attr('href'),函数(destino){
警报('destino='+destino);
});
BootstrapDialog.show({
标题:“可拖动对话框”,
消息:“destino=”+destino+”。请尝试拖动对话框标题以移动对话框。“”,
德拉格布尔:是的
});
});
});
在document.ready()中编写代码
$(文档).ready(函数(){
$('a')。单击(函数(){
$.get($('a').attr('href'),函数(destino){
警报('destino='+destino);
});
BootstrapDialog.show({
标题:“可拖动对话框”,
消息:“destino=”+destino+”。请尝试拖动对话框标题以移动对话框。“”,
德拉格布尔:是的
});
});
});

快速浏览…如果您想使用单击的
a
,请执行
$获取($(此).
而不是
$。获取($('a')…
。否则,请准确解释发生了什么(或者什么都没有发生?)并报告javascript控制台中显示的任何错误。除此之外,当我单击链接时,不会显示任何警报或对话框,中的页面只会显示在浏览器中。这就像忽略了此代码一样。
$('a').click(function(event){
        event.preventDefault();
        $.get($('a').attr('href'), function(destino){
            alert('destino = '+destino);
            BootstrapDialog.show({
                    title: 'Draggable Dialog',
                    message: 'destino = '+destino+' .Try to drag on dialog title to move your dialog.',
                    draggable: true
            });
        });

    });
<script>
    // The ready event handler is called after the page is ready.
    $().ready(function () {
        $('a').click(function(e) {
            e.preventDefault();

            $.get($(this).attr('href'), function(destino) {
                alert('destino = '+destino);
            });
            BootstrapDialog.show({
                title: 'Draggable Dialog',
                message: 'destino = '+destino+' .Try to drag on dialog title to move your dialog.',
                draggable: true
            });
        });
    });
</script>
  write your code inside document.ready()

   <script>
   $(document).ready(function(){
     $('a').click(function(){
      $.get($('a').attr('href'), function(destino){
        alert('destino = '+destino);
     });
     BootstrapDialog.show({
        title: 'Draggable Dialog',
        message: 'destino = '+destino+' .Try to drag on dialog title to move your dialog.',
        draggable: true
    });
   });
 });
</script>