doushuangai9733 2015-01-17 21:53
浏览 27
已采纳

Jquery .on()方法不使用$ .get()内容

I have a code like below:

$.get( "includes/ajax_ok.php", { edycja: "<?=$_GET[edycja]; ?>"} ,function( data ) {
    $('#lista').html(data);
});
$("document.body").on('click', '#test' ,function(){
    event.preventDefault();
    alert("ok");
});
<body>    
    <div id="lista"></div>
</body>

For test purposes, ajax_ok.php file has only a code line:

<a href='' id='test'>x</a>

The problem is, when i click an anchor x the alert with ok message doesn't appear.

I have tried code like below with succeed so I think the reason is $.get() function but I have to do this with it.

$('#lista').html("<a href='' id='test'>x</a>");

$("document.body").on('click', '#test' ,function(){
    event.preventDefault();
    alert("ok");
});

Maybe someone had the same problem but has a solution?

  • 写回答

2条回答 默认 最新

  • duankan6894 2015-01-17 21:54
    关注

    Use "body", not "document.body", the first arg to jQuery is always a CSS selector or HTMLDOMElement.

    You could also use document.body without the quotes.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 我这模型写的不对吗?为什么lingo解出来的下面影子价格这一溜少一个变量
  • ¥50 树莓派安卓APK系统签名
  • ¥15 maple软件,用solve求反函数出现rootof,怎么办?
  • ¥65 汇编语言除法溢出问题
  • ¥15 Visual Studio问题
  • ¥20 求一个html代码,有偿
  • ¥100 关于使用MATLAB中copularnd函数的问题
  • ¥20 在虚拟机的pycharm上
  • ¥15 jupyterthemes 设置完毕后没有效果
  • ¥15 matlab图像高斯低通滤波