将span标签id发送到URL,而不刷新页面

Send span tag id to the URL without refresh page

本文关键字:刷新 URL 标签 span id      更新时间:2023-09-26
<span id="50">Hello</span>

发送50到URL不刷新页面通过点击上面的span

$("span").click(function(){
    $(this).load("http://www.example.com/abc.php?q="+$(this).attr('id'), function(r,s,x){...});
});
$("span").click(function(){
      $.post("demo_.php",{
           Id:$(this).attr('id');
          },
      function(data,status){
          alert("Data: " + data + "'nStatus: " + status);
          });
});

响应ajax调用的abc.php的内容

<?php  
    //perform your work here with span id
    if(isset($_GET['value'])){
        $id = $_GET['value'];  //contains the span id sent using ajax
        $output['id']=$id; // I'm just going to echo the id
        header('Content-type: application/json');
        echo json_encode($output);
    }
?>

ajax调用的test.php的内容

<span id="50">Hello</span>
<script src="js/jquery-1.10.2.min.js"></script>
<script>
  jQuery(document).ready(function($) {
    $('span').click(function(e) {
        var value = $(this).attr('id');
        $.ajax({    
            url: 'abc.php', 
            type: 'get',
            data: {value:value}, 
            dataType: 'json',  
            success: function(rows){
                console.log('ajaX');
                        alert("from abc.php: value = " + rows['id']);
                    }
                });
            });
        });     
</script>