我有jquery函数从我的mysql数据库调用php脚本一些数据,jquery函数调用如下:
$(document).ready(function(){
getTopFiveDeals();
Run Code Online (Sandbox Code Playgroud)
当它运行时,它获得数据正常并构建一些HTML并将其插入到网页中.如下所示:
$('ul.special-list').append("<li><a href='#' class=restaurantItem restaurant= '" + item.estName + "' adddress='" + item.estAddr + "'><img src= " + item.img_link +
" width='60' height='60' alt='" + item.estName + "'><div class='img-det'><strong class='title'> " + item.title + " </strong><p> " + item.desc_short +
" <br>Expires: " + item.expiry_date + " </p><em class='price'>" + item.price + "</em></div></a><a href='dealDetail.html?id=" + item.id +
"' class='det-link'>Detail</a>");
Run Code Online (Sandbox Code Playgroud)
当我在下面有一个简单的jquery函数时,问题就开始了.该函数未被调用而是页面重新加载到index.html#
$("a.restaurantItem").click(function (event) {
alert('Hello');
}
Run Code Online (Sandbox Code Playgroud)
任何帮助和/或建议将不胜感激.
非常感谢
$("a.restaurantItem")被叫一次.它不寻找新的元素.您需要使用事件委托语法.on()来匹配这些动态创建的元素:
$('ul.special-list').on('click', 'a.restaurantItem', function(e) {
e.preventDefault(); // To stop the link from redirecting the browser
});
Run Code Online (Sandbox Code Playgroud)