【发布时间】:2012-07-30 07:00:57
【问题描述】:
我有一个 div,其内容是从 innerHTML 动态生成的,我似乎无法使用 jQuery 来定位这些元素进行验证,例如这是页面:
<html xmlns="http://www.w3.org/1999/xhtml" dir="ltr" lang="en-US">
<head>
</head>
<body>
<span id="alert1">Alert1</span>
<input type="button" value="Click Me" id="button1" />
<div id="empty"></div>
</body>
</html>
这是js代码:
<script type = "text/javascript">
$(document).ready(function() {
$('#button1').click(function() {
var html = "uname <input type='text' id='uname'><br />password <input type='text' id='password'><br /><span id='alert'>Alert</span>";
$('#empty').html(html);
});
$('#alert').click(function() {
alert('Alert');
});
$('#alert1').click(function() {
alert('Alert1');
});
});
</script>
id 为 alert1 的 span 的警报有效,但使用 innerHTML 或 .html() 设置的 id 警报不起作用,那么如何解决这个问题?
提前谢谢你
【问题讨论】: