开发者

How to use plugins for dynamically created elements in jQuery?

开发者 https://www.devze.com 2023-03-26 07:44 出处:网络
I don\'t know what to do. I want to make this: $(\'.message\').contextMenu(\'myMenu1\', { bindings: { \'open\': function(t) {

I don't know what to do. I want to make this:

$('.message').contextMenu('myMenu1', {
    bindings: {
        'open': function(t) {
            alert('Trigger was '+t.id+'\nAction was Open');
        },
        'email': function(t) {
            alert('Trigger was '+t.id+'\nAction was Email');
        },
        'save': function(t) {
            alert('Trigger was '+t.id+'\nAction was Save');
        },
        'delete': function(t) {
            alert('Trigger was '+t.id+'\nAction was Delete');
        }
    }
});

for dynamically created elements.

Elements with class message were added with help of AJAX:

    $.post('index.php', {link: link, ajax: true},
        function(response) {
            $("#tes开发者_如何转开发t").(response);
        }
    );

response is HTML code generated by server.

When Page loads firsrt, html code looks like:

<!DOCTYPE HTML>
<html lang="ru-RU">
<head>
    <meta charset="UTF-8">
    <title>RTIT chat</title>
    <link rel="stylesheet" type="text/css" href="view/styles/style.css" media="all" />
    <script type="text/javascript" src="view/scripts/jquery/jquery-1.6.2.min.js"> </script>
    <script type="text/javascript" src="view/scripts/jquery/jquery.test.js"> </script>
    <script type="text/javascript" src="view/scripts/jquery/jquery.contextmenu.r2.packed.js"> </script>
    <script type="text/javascript" src="view/scripts/controller.class.js"> </script>
</head>
<body>
    <div id="ajaxLoadPlace">
    </div>
</body>
</html>

Without elements with class message. Elements message will be generated with help of controller.class.js.

I tried this plugin on non dinamically created elements and it works fine!

How to do it?

This worked but only one time... after first updating of DOM.


Please post some more code and explain your question better. Why can you not just call that function the same time the element is dynamically created? If that's an issue, you could try this

$('.message').ready(function(){
    $(this).contextMenu('myMenu1', {
       bindings: {
        ...
        }
      }
    });
});

Update:

$.post('index.php', {link: link, ajax: true},
    function(response) {
        $("#test").(response);
        $(".message").contextMenu('myMenu1', {
           ...
     });
});
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号