Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
253 views
in Technique[技术] by (71.8m points)

javascript - How to catch creation of DOM elements and manipulate them with jQuery

I'm trying to devise a method of when adding a simple div element with a class and some data-* in it, it will replace it or add into it some other elements. This method should not be called manually, but automatically by some kind of .live() jQuery method, a custom event or some kind like $('body').bind('create.custom'), etc. I need it this way since I wouldn't know in advance what elements will be created since they will be served through ajax like single empty div's or p's .

<!DOCTYPE html>
<html>
    <head>

        <title >on create</title>

        <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.min.js" ></script>

        <script type="text/javascript" >

            jQuery(function($){
                $("div.fancyInput").each(function(index,element){

                    var $div = $(this);
                    var dataId = $div.attr("data-input-id");
                    var inputId = '';
                    var labelId = '';
                    if(!!dataId){
                        inputId = 'id="' + dataId + '"';
                        labelId = 'id="' + dataId + 'Label"';
                    } // if

                    var dataValue = $div.attr();

                    $(
                        '<p class="fancyInput" >' +
                        '    <label ' + labelId + ' for="' + inputId + '" >A fancy input</label>' +
                        '    <input ' + inputId + ' name="' + inputId + '" value="A fancy input" />' +
                        '</p>'
                    ).appendTo($div);               
                }); // .each()
            }); // jQuery()
        </script>

        <script type="text/javascript" >

            jQuery(function($){
                var counter = 2;
                var $form = $('#form');
                $('#add').click(function(event){
                    $('<div class="fancyInput" data-input-id="fancyInput' + counter + '" ></div>').appendTo($form);
                    counter++;
                }); // .click
            }); // jQuery()

        </script>
    </head>
    <body>

        <a id="add" href="#" > add another one </a>
        <form id="form" action="#" >

            <p class="normalInput" >
                <label id="normalInputLabel" for="normalInput" >A normal input</label>
                <input id="normalInput" name="normalInput" value="A normal input" />
            </p>

            <div class="fancyInput" ></div>

        </form>
    </body>
</html>

Update: I checked liveQuery beforehand, it's that kind of functionality that I need, but with the ability to modify DOM elements while the event callback is executed. So it's not just that I need events attached, but the ability to modify the DOM upon element creation. For example: whenever a new is created, it should be filled in (even better if replaced) with the p, label and input tags

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You could use a DOM Level 3 Event, like DOMNodeInserted. This could look like:

$(document).bind('DOMNodeInserted', function(event) {
    // A new node was inserted into the DOM
    // event.target is a reference to the newly inserted node
});

As an alternative, you might checkout the .liveQueryhelp jQuery plugin.

update

In referrence to your comment, have a look at http://www.quirksmode.org/dom/events/index.html, only browser which do not support it are the Internet Explorers of this this world (I guess IE9 does at least).

I can't say much about the performance, but it should perform fairly well.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...