开发者

How to use on the fly jquery object with jquery template?

开发者 https://www.devze.com 2023-03-13 12:14 出处:网络
my jquery template displays [object Object] when I pass the following jquery anchor object. moreinfoAnchor = $(\"<a></a>\");

my jquery template displays [object Object] when I pass the following jquery anchor object.

    moreinfoAnchor = $("<a></a>");
    moreinfoAnchor.attr('id', 'moreLink-' + this.id);
    moreinfoAnchor.text("test");

jquery template code:

$("#test开发者_如何学C").tmpl({
   link: moreinfoAnchor
});

How do I display actual anchor with jquery template?


Here's one way to do it.

Build your anchor tag with jQuery:

var moreinfoAnchor = $("<a></a>")
  .attr({
    'href' : 'http://www.example.com',
    'id' : 'moreLink'
  })
  .text("test");

Turn your jQuery anchor tag into a plain string:

var plainString = $('<div>')
  .append( moreinfoAnchor.clone() ).remove().html();

Then apply the template:

$("#test").tmpl({
  link: plainString
}).appendTo('#thediv');

You'll also have to make sure your template is set up using the html template tag, something like this:

<script id="test" type="text/x-jquery-tmpl">
  <div>{{html link}}</div>
</script>
0

精彩评论

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