一个jQuery函数将html内容附加到div元素.但是html元素无法在头部访问jquery函数 [英] a jquery function appended html content to a div element. but that html elements cannot access a jquery function in the head section

查看:91
本文介绍了一个jQuery函数将html内容附加到div元素.但是html元素无法在头部访问jquery函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以前,我将问题和所有详细信息放到stackoverflow中.但我没有任何帮助.所以现在我使我的情况变得简单,并在这里再次提出我的问题.

Previously I put my question in stackoverflow with all details. but I couldn't get any help. so now I am making my scenario simple and put my question again here.

我有三个php文件.

I have three php files.

index.php
request.php
test.php

这是Index.php文件.在此文件头中,我有两个简单的jquery函数.一种功能是将单独的php文件加载到#editor-form-container div标签中.其他函数只是在调用时发出警报.

This is the Index.php file. In this file header I have two simple jquery functions. one function is loading separate php file in to the #editor-form-container div tag. other function is just giving an alert when it called.

在此页面的正文中,我有一个div标签,其文本为"click me for button",一旦单击该div,该div就会调用posting_url()函数,并且该函数将如前所述加载外部php文件.

In the body of this page I have a div tag having the text "click me for button" and this div is calling the posting_url() function once it clicked and that function is loading external php file as describe before.

<html>
    <head>
        <meta charset="UTF-8">
        <title></title>
        <script src="jquery.min.js" type="text/javascript"></script>
        <script>
            function posting_url() {
                var url = 'request.php';
                var pdata = "";
                $.ajax({
                    url: url,
                    data: pdata,
                    success: function(data) {
                        $('#editor-form-container').html(data);
                    }
                });
            }
            $(document).ready(function() {
                $('.click_me_for_alert').on('click', function() {
                    alert("You clicked a button.");
                });
            });
        </script>
    </head>
    <body>
        <?php
        // put your code here
        ?>
        <div onclick="posting_url()">
            Click me for button
        </div>
        <div id="editor-form-container">
            <button class="click_me_for_alert">
                Iam a default button
            </button>
        </div>
    </body>
</html>

这是request.php文件.它只需要test.php文件

this is the request.php file. it is just requiring the test.php file

<?php.
require_once 'teset.php';
?>.

然后在test.php文件中.我有一个简单的按钮.像这样.

then in the test.php file. I have a simple button. like this.

<button class="click_me_for_alert">
    Iam a appended button
</button>

在单击div之前,如果单击默认"按钮,则文本为单击按钮"的div.然后它给了我警报.

Before I click the div that having the text "Click me for button" if I click the Iam a default button. then it gives me the alert.

但是问题出在我单击div标签"Click me for button",并且在Iam之后,如果我单击该按钮,则会出现一个附加按钮,这没有给我警报.没有控制台错误.

But the problem is after I click the div tag "Click me for button" and after the Iam a appended button appears if I click that button it is not giving me that alert. no console errors.

为什么会这样?

推荐答案

对于动态创建的元素,请使用委托事件处理程序:

For dynamically created elements use delegated event handler:

$(document).ready(function()
{
    $("#editor-form-container").on('click', '.click_me_for_alert', function()
    {
        alert("You clicked a button.");
    });
});

这篇关于一个jQuery函数将html内容附加到div元素.但是html元素无法在头部访问jquery函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆