HTML Generation in JS Code

I would like to know your thoughts on generating HTML code in my JS code.

I just think the style is html.push("<tag>" + something + "</tag>")pretty annoying. I already tried something with templates inside my HTML file (and put some placeholders in it), and then used its contents to replace the placeholders with my real values.

But maybe you have other ideas, maybe using jQuery.

+3
source share
6 answers

jQuery has javascript template plugins like jBind and jTemplate . I have not used them myself, but I recommend jQuery whenever possible.

Note about generating HTML code in most cases is not supported by search engines.

+3

jQuery - . :

// create some HTML
var mySpan = $("<span/>").append("Something").addClass("highlight");
  • -,

templating .

+5

, PrototypeJS .

Template , HTML.

var tpl = new Template('Here is a link to <a href="#{link}">#{sitename}</a>');

, .

$('someDiv').innerHTML = tpl.evaluate( {link: 'http://www.stackoverflow.com', sitename: 'StackOverflow'} );

div id = "someDiv", div .

+3

Resig has a small blog post to create a very lightweight template system.

+3
source

There is a jQuery function that supports this. In particular, you should look at append(content), appendTo(content) prepend(content) prependTo(content), replaceWith(content), after(content), before(content), insertAfter(content)and insertBefore(content).

0
source

Source: https://habr.com/ru/post/1709807/


All Articles