How to wrap jQuery function in a div?

How can third-party code use jQuery DOM API?

  • Consider the following scenario: 1. Host Page <!doctype html> <html> <head> <meta charset="GBK"/> <script src="jquery-min.js"></script> <script src="http://localhost:8080/caja.js"></script> </head> <body> <div id="module" style="position: relative; overflow: auto;"> <h3 id="m-title" class="m-title-cl">title</h3> </div> <script> caja.configure({ cajaServer: 'http://localhost:8080', debug: true }, function(frameGroup) { var shared = (function (x) { return { //expose the jquery selector function to third-party code get: frameGroup.markFunction(function(selector) { return frameGroup.tame($(selector)); }) }; })(""); frameGroup.makeES5Frame(document.getElementById('module'), function(frame) { frame.url('http://localhost/balcony/docs/demo/cases/tuangou/ module-c.html').run({ JQUERY: frameGroup.tame(shared) }); }); }); </script> </body> </html> 2. third-party code <p id="content">This is Module Content</p> <script> JQUERY.get("#m-title"); // output: undefined, which means it can't get dom element inside the container in this way. JQUERY.get("#content"); // output: null, which I know, all ids of elements inside container will be rewritten into id_- CajaGadget-0___ </script> So , here comes the problem: I can't provide a lib to third-party code to simplify its dom operation but only let the third-party code use document.getElementById to get dom. Are there any ways to make the dom operation more convenient for third-party code by exposing a lib to it such as jQuery?

  • Answer:

    One use case is : jQuery.getJSON( url [, data] [, success(data, textStatus, jqXHR)] ) example : Loads the four most recent cat pictures from the Flickr JSONP API. 1 2 3 4 5 6 7 8 91011121314151617181920212223242526<!DOCTYPE html> <html> <head>   <style>img{ height: 100px; float: left; }</style>   <script src="http://code.jquery.com/jquery-latest.js"></script> </head> <body>   <div id="images"> </div> <script> $.getJSON("http://api.flickr.com/services/feeds/photos_public.gne?jsoncallback=?",   {     tags: "cat",     tagmode: "any",     format: "json"   },   function(data) {     $.each(data.items, function(i,item){       $("<img/>").attr("src", item.media.m).appendTo("#images");       if ( i == 3 ) return false;     });   });</script> </body> </html> For more cf: http://www.jquery4u.com/json/ajaxjquery-getjson-simple/

Sameer Gupta at Quora Visit the source

Was this solution helpful to you?

Related Q & A:

Just Added Q & A:

Find solution

For every problem there is a solution! Proved by Solucija.

  • Got an issue and looking for advice?

  • Ask Solucija to search every corner of the Web for help.

  • Get workable solutions and helpful tips in a moment.

Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.