dousui7410 2014-02-14 13:27
浏览 99
已采纳

JQuery:从Html页面检索特定用户数据

I am trying to get the specific username information from the links below. When one of the links is clicked, the username information should be kept in the var username. However, it is not working. Any help is very welcome.

<!doctype html>
<?php  
$theUsernameDaniel="danielcajueiro";
$theUsernameMarcelo="marcelopapini";
?>
<html>
    <head>
        <meta charset="utf-8" />
        <title>ControllingHiperlinks</title>
        <script src="http://code.jquery.com/jquery-1.4.1.js" type="text/javascript"></script>  
        <script>
            $(document).ready(function() {
                $("a.peoplePage").click(function(event) {

                    var username=$(this).data("username");

                    alert(username);
                    event.preventDefault();
                });
            });
        </script>        
    </head>
    <body>
        <!-- Updated after the answer below of @Pranav-C-Balan -->
        <a class="peoplePage" data-username="<?php echo $theUsernameDaniel ?>"  href=""> Daniel Cajueiro</a>
        <a class="peoplePage" data-username="<?php echo $theUsernameMarcelo ?>"  href="">Marcelo Cajueiro</a>
    </body>
</html>
  • 写回答

4条回答 默认 最新

  • dongmeng1868 2014-02-14 13:29
    关注

    Missing "" in your html code

    <a class="peoplePage" data-username="<?php echo $theUsernameDaniel ?>"  href=""> Daniel Cajueiro</a>
    <!--................................^................................^.............................-->
    
    <a class="peoplePage" data-username="<?php echo $theUsernameMarcelo ?>"  href="">Marcelo Cajueiro</a>
    <!--................................^................................^.............................-->
    

    Use latest version of jQuery library

    <script src="http://code.jquery.com/jquery-1.9.1.js" type="text/javascript"></script>
    

    Otherwise use

    var username=$(this).attr("data-username");
    

    instead of

    var username=$(this).data("username");
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 51单片机中C语言怎么做到下面类似的功能的函数(相关搜索:c语言)
  • ¥15 seatunnel 怎么配置Elasticsearch
  • ¥15 PSCAD安装问题 ERROR: Visual Studio 2013, 2015, 2017 or 2019 is not found in the system.
  • ¥15 (标签-MATLAB|关键词-多址)
  • ¥15 关于#MATLAB#的问题,如何解决?(相关搜索:信噪比,系统容量)
  • ¥500 52810做蓝牙接受端
  • ¥15 基于PLC的三轴机械手程序
  • ¥15 多址通信方式的抗噪声性能和系统容量对比
  • ¥15 winform的chart曲线生成时有凸起
  • ¥15 msix packaging tool打包问题