我正在从数据库加载一些html(使用ajax post请求),它看起来像:
<div id="slides_control">
<div>
<a href="#"></a>
</div>
</div>
使用 html,我还加载 JS 代码:
<script>
$('#slides_control a').bind('click', function() {
alert('achtung');
});
</script>
脚本紧接在 html 之后(在接收到的数据中)。
但是当我单击新 html 中的某个链接时,我没有看到警报。怎么了?
<小时/>我也尝试在ajax结束后绑定(bind)它:
$.post('page.php', {}, function(data) {
document.write(data);
$('#slides_control a').bind('click', function() {
alert('achtung');
});
});
没有帮助我。
最佳答案
您可能在加载 html 之前运行绑定(bind)函数,因此它找不到元素
所以,让你的代码在 dom 加载上运行:
$(function(){
$('#slides_control a').bind('click', function() {
alert('achtung');
});
}):
关于javascript - JQuery 事件绑定(bind),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5858228/