>웹 프론트엔드 >HTML 튜토리얼 >Fluent HTML Tag Builder_html/css_WEB-ITnose

Fluent HTML Tag Builder_html/css_WEB-ITnose

WBOY
WBOY원래의
2016-06-24 11:27:051023검색

来自: https://github.com/rwhitmire/tag-builder

tag-builder

Fluent html tag building library.

Install

npm install --save tag-builder

Examples

const assert = require('assert')const TagBuilder = require('tag-builder')const div = new TagBuilder('div')const html = div.text('hello world').toString()assert.equal(html, '<div>hello world</div>')
const html = TagBuilder  .create('input')  .attr({    'type': 'password',    'name': 'password'  })  .addClass('form-control')  .toString()assert.equal(html, '<input type="password" name="password" class="form-control">')
const ul = TagBuilder.create('ul')const li1 = TagBuilder.create('li').text('1')const li2 = TagBuilder.create('li').text('2')ul.appendHtml(li1)ul.appendHtml(li2)const html = ul.toString()assert.equal(html, '<ul><li>1</li><li>2</li></ul>')

Seetests for more examples.

API

All methods except toString() return a TagBuilder instance and are chainable.

create(tagName: string)

Returns a TagBuilder instance.

html(html: any)

Sets innerHTML of the element. You may pass either a string or TagBuilder instance to this method.

appendHtml(html: any)

Appends provided html to inner content. You may pass either a string or TagBuilder instance to this method.

text(text: string)

Sets html encoded innerText of the element.

appendText(text: string)

Appends html encoded text to inner content.

attr(attributes: object)

Merges attributes hash onto the tag.

addClass(className: string)

Merges className onto the tag.

toString()

Returns html string.

성명:
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.