Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/83.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript jquery事件不适用于ajax加载的内容(html)_Javascript_Jquery - Fatal编程技术网

Javascript jquery事件不适用于ajax加载的内容(html)

Javascript jquery事件不适用于ajax加载的内容(html),javascript,jquery,Javascript,Jquery,当我使用ajax加载html内容时,加载的html上的jquery事件不起作用 比如说- 我有一个jquery函数- $('.test').on('click', function(){ alert('hi'); }); ajax响应: <div> <p class="text">test data</p> </div> 测试数据 现在,单击ajax加载的数据并不像我定义的那样在单击事件时触发jquery。请尝试一下,希望能有所帮助

当我使用ajax加载html内容时,加载的html上的jquery事件不起作用

比如说-

我有一个jquery函数-

$('.test').on('click', function(){
  alert('hi');
});
ajax响应:

<div>
  <p class="text">test data</p>
</div>

测试数据


现在,单击ajax加载的数据并不像我定义的那样在单击事件时触发jquery。

请尝试一下,希望能有所帮助

$(document).ready(function() { 
   $.get(url, {}, function(response) {
     $('#ajax-loadedData').html(response); 
     bindEventOnLoadedData();
  });
}); 

function bindEventOnLoadedData() {
   $('.test').on('click', function(){
    alert('hi');
   });
}

“测试”!='文本“
?事件处理程序仅绑定到文档中
$('.test')时存在的元素。在('click',函数(){
)上执行。请阅读事件委派以了解处理此问题的简单方法。