简体   繁体   中英

How to let the user chose output markup in jQuery plugin development

I develop a jQuery plugin which produces HTML output, currently something like

<ul>
  <li><img></img></li>
  ...
</ul>

But actually, I would like to let the user of my plugin chose the HTML markup which gets generated, so for instance he passes a template string from which my plugin produces the output. But I don't know how.

Are there any best practices / "patterns" on how to achieve such a functionality?

The easiest way is to allow for an optional rendering function param and pass it the element(s). This way you have maximum control with minimal effort. Example:

$('#id').my_action(param1, function (e1, e2) {
    $('#target1').append(e1);
    $('#target2').append(e2);
});

Take a look at the Fluid Renderer . I believe they're doing something very similar to what you describe.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM