开发者

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

开发者 https://www.devze.com 2022-12-18 22:11 出处:网络
I develop a jQuery plugin which produces HTML output, currently something like <ul> <li><img></img></li>

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 / "patte开发者_如何学JAVArns" 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.

0

精彩评论

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