weixin_33725239 2012-10-30 11:13 采纳率: 0%
浏览 85

使Ajax取消超链接

Is there a way to make an Ajax call from a hyperlink?

I'd like to change the inner html of a div element when someone clicks on a hyperlink using javascript.

  • 写回答

2条回答 默认 最新

  • weixin_33720956 2012-10-30 11:15
    关注

    Ajax is just "something you can do with JavaScript"

    Hyperlinks can have event handlers bound on them like any other HTML element.

    var links_in_document = document.getElementsByTagName('a');
    var a_link = links_in_document[0];
    a_link.addEventListener('click', a_function_that_does_something_ajaxy);
    
    function a_function_that_does_something_ajaxy(e) {
        var xhr = new XMLHttpRequest();
        // etc
        e.preventDefault();
    }
    

    Note: No compatibility routines for browsers that don't support standards are included in this code sample. You should add some (along with a more sensible way to determine which link you are targeting).

    评论

报告相同问题?

悬赏问题

  • ¥15 如何用stata画出文献中常见的安慰剂检验图
  • ¥15 c语言链表结构体数据插入
  • ¥40 使用MATLAB解答线性代数问题
  • ¥15 COCOS的问题COCOS的问题
  • ¥15 FPGA-SRIO初始化失败
  • ¥15 MapReduce实现倒排索引失败
  • ¥15 ZABBIX6.0L连接数据库报错,如何解决?(操作系统-centos)
  • ¥15 找一位技术过硬的游戏pj程序员
  • ¥15 matlab生成电测深三层曲线模型代码
  • ¥50 随机森林与房贷信用风险模型