Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/434.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 我无法单击ajax生成的div_Javascript_Jquery_Ajax - Fatal编程技术网

Javascript 我无法单击ajax生成的div

Javascript 我无法单击ajax生成的div,javascript,jquery,ajax,Javascript,Jquery,Ajax,我尝试了不同的解决方案,但仍然不适合我,请帮助我 function get_category(){ var xmlhttp = new XMLHttpRequest(); xmlhttp.onreadystatechange = function() { if (xmlhttp.readyState == 4 && xmlhttp.status == 200) { var ans = document.getElementById("sell_panel"); ans.inner

我尝试了不同的解决方案,但仍然不适合我,请帮助我

function get_category(){
var xmlhttp = new XMLHttpRequest();
xmlhttp.onreadystatechange = function() {
if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
var ans = document.getElementById("sell_panel");
ans.innerHTML  = xmlhttp.responseText;
}
}
xmlhttp.open("GET", "includes/get_category.php", true);
xmlhttp.send();

}
`$('.item_btn').on('click', function () {// HERE IS MY CLICK EVENT
alert("helo");
});`

AND MY get_category.PHP FILE IS;

<?php
include("database.php");
$db = new Database();   
$sql = "SELECT DISTINCT category FROM stock";
$result = mysql_query($sql);
if($result === FALSE) { 
die(mysql_error()); // TODO: better error handling
}   
while($cat = mysql_fetch_array($result)){

echo "
<div class='item_btn' id='{$cat[0]}'>//HERE IS THE DIV

<label>{$cat[0]}</label>

</div>  
";
}   

?>
函数get_category(){
var xmlhttp=new XMLHttpRequest();
xmlhttp.onreadystatechange=函数(){
if(xmlhttp.readyState==4&&xmlhttp.status==200){
var ans=document.getElementById(“销售面板”);
ans.innerHTML=xmlhttp.responseText;
}
}
open(“GET”,“includes/GET_category.php”,true);
xmlhttp.send();
}
`$('.item_btn')。在('click',函数(){//这是我的click事件
警报(“直升机”);
});`
我的get_category.PHP文件是;

尝试将事件附加到父级而不是
。项\u btn

$('body').on('click','.item_btn', function () {
   alert("helo");
});

如果较旧的jQuery尝试使用
.live()
而不是
.on()


非常感谢,我丢失了$(文档)。on()非常感谢,它解决了我的许多项目。hank.on()很好,非常感谢
$( selector ).live( events, data, handler );                // jQuery 1.3+
$( document ).delegate( selector, events, data, handler );  // jQuery 1.4.3+
$( document ).on( events, selector, data, handler );        // jQuery 1.7+