jquery 或 javascript 在事件上动态加载时会执行吗?

发布于 2024-12-12 09:02:14 字数 1148 浏览 4 评论 0原文

我有一个下拉菜单,当我选择一个选项时,它将从服务器加载一个适当的表并使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

め七分饶幸 2024-12-19 09:02:14

DEMO

您的 id 后面有一个空格

(而不是

所以这个是正确的

    <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>

您也可以只使用警报中的索引来抓取号码。你不需要“我”

DEMO

You have a space after your id

(<table id="dataFileTableHeader "> instead of <table id="dataFileTableHeader">

So this one is correct

    <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>

You can also just use the index in the alert to grab the number. You don't need "i"

人心善变 2024-12-19 09:02:14

如果您的内容是动态加载的,则脚本可能会在内容到达那里之前运行。将此代码放在 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?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文