├── tests
├── ajax.htm
├── index.htm
├── qunit.css
├── tests.js
└── qunit.js
├── README.markdown
├── example.htm
└── jquery.create.js
/tests/ajax.htm:
--------------------------------------------------------------------------------
1 |
22 |
jQuery "create" Event Demo
23 |
24 |
This new event duck punches $.fn.domManip and $.fn.html to fire a custom "create" event when new, matching elements are inserted into the DOM.
25 |
26 |
30 |
31 |
Code for this demo:
32 |
33 |
34 | $.fn.geocities(function(){
35 | /*
36 | code to make a div look friggin sweet.
37 | wraps text in marquee and applies a
38 | beautiful color scheme.
39 | */
40 | });
41 |
42 | // apply plugin to all current div.geocities
43 | $("div.geocities").geocities();
44 |
45 | // apply plugin to all future div.geocities
46 | $("div.geocities").live("create", function(e){
47 | $(this).text("I am a new element!").geocities();
48 | });
49 |
50 | // the "create another" link
51 | $("#create-new-div").click(function(){
52 | $("div.geocities:last").after('<div class="geocities"></div>');
53 | return false;
54 | });
55 |
56 |
57 |
Create another
58 |
Welcome to 1999. Look at me go!!
59 |
60 |
61 |
62 |
63 |
64 |
65 |
93 |
94 |
95 |
96 |
--------------------------------------------------------------------------------
/tests/qunit.css:
--------------------------------------------------------------------------------
1 |
2 | ol#qunit-tests {
3 | font-family:"Helvetica Neue Light", "HelveticaNeue-Light", "Helvetica Neue", Calibri, Helvetica, Arial;
4 | margin:0;
5 | padding:0;
6 | list-style-position:inside;
7 |
8 | font-size: smaller;
9 | }
10 | ol#qunit-tests li{
11 | padding:0.4em 0.5em 0.4em 2.5em;
12 | border-bottom:1px solid #fff;
13 | font-size:small;
14 | list-style-position:inside;
15 | }
16 | ol#qunit-tests li ol{
17 | box-shadow: inset 0px 2px 13px #999;
18 | -moz-box-shadow: inset 0px 2px 13px #999;
19 | -webkit-box-shadow: inset 0px 2px 13px #999;
20 | margin-top:0.5em;
21 | margin-left:0;
22 | padding:0.5em;
23 | background-color:#fff;
24 | border-radius:15px;
25 | -moz-border-radius: 15px;
26 | -webkit-border-radius: 15px;
27 | }
28 | ol#qunit-tests li li{
29 | border-bottom:none;
30 | margin:0.5em;
31 | background-color:#fff;
32 | list-style-position: inside;
33 | padding:0.4em 0.5em 0.4em 0.5em;
34 | }
35 |
36 | ol#qunit-tests li li.pass{
37 | border-left:26px solid #C6E746;
38 | background-color:#fff;
39 | color:#5E740B;
40 | }
41 | ol#qunit-tests li li.fail{
42 | border-left:26px solid #EE5757;
43 | background-color:#fff;
44 | color:#710909;
45 | }
46 | ol#qunit-tests li.pass{
47 | background-color:#D2E0E6;
48 | color:#528CE0;
49 | }
50 | ol#qunit-tests li.fail{
51 | background-color:#EE5757;
52 | color:#000;
53 | }
54 | ol#qunit-tests li strong {
55 | cursor:pointer;
56 | }
57 | h1#qunit-header{
58 | background-color:#0d3349;
59 | margin:0;
60 | padding:0.5em 0 0.5em 1em;
61 | color:#fff;
62 | font-family:"Helvetica Neue Light", "HelveticaNeue-Light", "Helvetica Neue", Calibri, Helvetica, Arial;
63 | border-top-right-radius:15px;
64 | border-top-left-radius:15px;
65 | -moz-border-radius-topright:15px;
66 | -moz-border-radius-topleft:15px;
67 | -webkit-border-top-right-radius:15px;
68 | -webkit-border-top-left-radius:15px;
69 | text-shadow: rgba(0, 0, 0, 0.5) 4px 4px 1px;
70 | }
71 | h2#qunit-banner{
72 | font-family:"Helvetica Neue Light", "HelveticaNeue-Light", "Helvetica Neue", Calibri, Helvetica, Arial;
73 | height:5px;
74 | margin:0;
75 | padding:0;
76 | }
77 | h2#qunit-banner.qunit-pass{
78 | background-color:#C6E746;
79 | }
80 | h2#qunit-banner.qunit-fail, #qunit-testrunner-toolbar {
81 | background-color:#EE5757;
82 | }
83 | #qunit-testrunner-toolbar {
84 | font-family:"Helvetica Neue Light", "HelveticaNeue-Light", "Helvetica Neue", Calibri, Helvetica, Arial;
85 | padding:0;
86 | /*width:80%;*/
87 | padding:0em 0 0.5em 2em;
88 | font-size: small;
89 | }
90 | h2#qunit-userAgent {
91 | font-family:"Helvetica Neue Light", "HelveticaNeue-Light", "Helvetica Neue", Calibri, Helvetica, Arial;
92 | background-color:#2b81af;
93 | margin:0;
94 | padding:0;
95 | color:#fff;
96 | font-size: small;
97 | padding:0.5em 0 0.5em 2.5em;
98 | text-shadow: rgba(0, 0, 0, 0.5) 2px 2px 1px;
99 | }
100 | p#qunit-testresult{
101 | font-family:"Helvetica Neue Light", "HelveticaNeue-Light", "Helvetica Neue", Calibri, Helvetica, Arial;
102 | margin:0;
103 | font-size: small;
104 | color:#2b81af;
105 | border-bottom-right-radius:15px;
106 | border-bottom-left-radius:15px;
107 | -moz-border-radius-bottomright:15px;
108 | -moz-border-radius-bottomleft:15px;
109 | -webkit-border-bottom-right-radius:15px;
110 | -webkit-border-bottom-left-radius:15px;
111 | background-color:#D2E0E6;
112 | padding:0.5em 0.5em 0.5em 2.5em;
113 | }
114 | strong b.fail{
115 | color:#710909;
116 | }
117 | strong b.pass{
118 | color:#5E740B;
119 | }
120 |
--------------------------------------------------------------------------------
/jquery.create.js:
--------------------------------------------------------------------------------
1 | // written by eric hynds (erichynds.com)
2 | // http://www.erichynds.com/jquery/jquery-create-event/
3 | // version 1.4 - 10/12/2010
4 |
5 | (function($, _domManip, _html){
6 | var selectors = [], gen = [], guid = 0, old = {};
7 |
8 | $.event.special.create = {
9 | add: function( data ){
10 | selectors.push( data.selector );
11 | },
12 |
13 | // won't fire in 1.4.2 http://dev.jquery.com/ticket/6202
14 | remove: function( data ){
15 | var len = selectors.length;
16 |
17 | while( len-- ){
18 | if( selectors[len] === data.selector ){
19 | selectors.splice(len, 1);
20 | break;
21 | }
22 | }
23 | }
24 | };
25 |
26 | // deal with 99% of DOM manip methods
27 | $.fn.domManip = function( args, table, callback ){
28 |
29 | // if no create events are bound, just fire the original domManip method
30 | if( !selectors.length || $.isFunction(args[0]) ){
31 | return _domManip.apply( this, arguments );
32 | }
33 |
34 | return logic.call( this, _domManip, arguments );
35 | };
36 |
37 | // deal with the remaining 1% (html method)
38 | $.fn.html = function( value ){
39 |
40 | // if no create events are bound, html() is being called as a setter,
41 | // or the value is a function, fire the original and peace out. only string values use innerHTML;
42 | // function values use append() which is covered by $.fn.domManip
43 | if( !selectors.length || $.isFunction(value) || typeof value === "undefined" || !value.length ){
44 | return _html.apply( this, arguments );
45 | }
46 |
47 | // make value an array
48 | arguments[0] = [value];
49 | return logic.call( this, _html, arguments );
50 | };
51 |
52 | function logic( method, args ){
53 | var node, nodes = args[0], html = $(), numSelectors = selectors.length, matches = [];
54 |
55 | // crawl through the html structure passed in looking for matching elements.
56 | for( var i=0, len=nodes.length; i