jquery 或 javascript 在事件上动态加载时会执行吗?
我有一个下拉菜单,当我选择一个选项时,它将从服务器加载一个适当的表并使用 jQuery 显示它,与该表一起,我还发送一个小的 jQuery 脚本,例如,
<table id="dataFileTableHeader">
<thead>
<tr>
<th><strong>Export Type</strong></th>
<th><strong>Company</strong></th>
<th><strong>File Name</strong></th>
<th><strong>Date Modified</strong></th>
<th><strong>Total Records</strong></th>
<th><strong>File Size</strong></th>
<th><strong>Owner</strong></th>
</tr>
</thead>
</table>
<script>
$(function(){
var i = 0;
$('#dataFileTableHeader th').each(function(index) {
alert("hello " + (++i));
});
});
</script>
加载时我期待 alert< /code> 显示 7 次但没有任何反应,我是否错过了什么?
I have a dropdown and when i choose an option it will load an appropriate table from the server and display it using jQuery, along with the table i also send a tiny jQuery script like,
<table id="dataFileTableHeader">
<thead>
<tr>
<th><strong>Export Type</strong></th>
<th><strong>Company</strong></th>
<th><strong>File Name</strong></th>
<th><strong>Date Modified</strong></th>
<th><strong>Total Records</strong></th>
<th><strong>File Size</strong></th>
<th><strong>Owner</strong></th>
</tr>
</thead>
</table>
<script>
$(function(){
var i = 0;
$('#dataFileTableHeader th').each(function(index) {
alert("hello " + (++i));
});
});
</script>
when this is loaded i am expecting alert
to show up 7 times but nothing happens am i missing something?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
DEMO
您的 id 后面有一个空格
(
而不是
所以这个是正确的
您也可以只使用警报中的索引来抓取号码。你不需要“我”
DEMO
You have a space after your id
(
<table id="dataFileTableHeader ">
instead of<table id="dataFileTableHeader">
So this one is correct
You can also just use the index in the alert to grab the number. You don't need "i"
如果您的内容是动态加载的,则脚本可能会在内容到达那里之前运行。将此代码放在 ajax 函数中的“成功”参数之后(如果没有看到该代码,很难说)。有道理吗?
If your content is dynamically loaded, the script is probably being run before the content gets there. Have this code after your "success" parameters in your ajax function (hard to say without seeing that code). Make sense?