elle
a small DOM element wrapper
Last updated 6 years ago by mattdesl .
MIT · Repository · Bugs · Original npm · Tarball · package.json
$ cnpm install elle 
SYNC missed versions from official npm registry.

elle

stable

A tiny DOM utility that wraps a few common features in a chainable structure. Targets modern browsers.

  • construct from string template (see domify)
  • set CSS styling (see dom-css)
  • insertion/removal (see insert)
  • add/remove classes (see dom-classes)
  • aliases for innerHTML, setAttribute/getAttribute and parentNode

Other features are left up to the user to implement or extend (see examples/).

The main purpose is to provide a thin layer for other components/frameworks to build on. This is better suited for prototyping and higher-level frameworks, rather than small and focused modules.

example

Basic example:

require('elle')(document.body)
  .css({
    background: 'black',
    margin: 20,                  //auto-px'd 
    fontSmoothing: 'antialiased' //vendor-prefixed
  })
  .append('<div><a href="#">click</a></div>')

Another example:

//style and append text content to the body
var body = elle(document.body)
  .css('margin', 20)
  .append('some, ', 'text')

//creates a bare <div> with some styles
//then attach it to the body
elle()
    .addClass('foobar')
    .css({
      width: 20,
      height: 20,
      background: 'blue'
    })
    .appendTo(body)

//creates a Text node and DocumentFragment
var text = elle('text node')
var frag = elle('<div>a</div><div>document fragment</div>')

//attach the nodes to the body
elle(body)
  .append(text)
  .append(frag)

overview

  • manipulation
    • append()
    • prepend()
    • before()
    • after()
    • insertBefore()
    • insertAfter()
    • appendTo()
    • prependTo()
  • classes
    • hasClass()
    • addClass()
    • removeClass()
    • toggleClass()
  • misc
    • css() - applies dom style with dom-css
    • attr() - getter/setter for setAttribute
    • html() - getter/setter for innerHTML
    • parent() - returned a wrapped parentNode
    • view - returns the underlying DOM node

Usage

NPM

e = elle([opt])

Creates a new element wrapper with the given options. If nothing is given, the element will default to an empty <div>.

If a string is specified, it will create a different type depending on the contents. e.g.

  • "some text" results in a Text node
  • "<div>text</div>" results in a <div> with a Text node
  • "<div></div><div></div>" results in a DocumentFragment

You can also pass an element (like document.body etc), or any object that contains a view node (like elle objects).

e.view

Returns the underlying DOM node for this wrapper. You can also access the node with array dereference, as in:

elle(document.body)[0] === document.body

e.append(content)

e.prepend(content)

e.before(content)

e.after(content)

Inserts content (which can be an element, or a string, or an array of elements/strings) into the wrapped element. Returns this for chaining.

e.insertBefore(target)

e.insertAfter(target)

e.appendTo(target)

e.prependTo(target)

Inserts the wrapped target into the specified target element. Returns this for chaining.

e.remove()

Removes this element from its parent. Returns this for chaining.

e.parent()

Returns the parent node, also wrapped as an elle object.

e.hasClass(name)

Returns true if the element contains the given class. Uses a fallback for non-classList browsers.

e.addClass(name)

e.toggleClass(name)

e.removeClass(name)

Adds/removes/toggles a single class by string name.

If a RegExp is given to removeClass, it will remove all classes that match.

e.css(map)

e.css(name, value)

Applies styling with dom-css. You can provide an object of name:value pairs, or specify each property name and value individually.

e.html([value])

A getter/setter for innerHTML. If value is a string, it will set innerHTML (returning this for chaining), otherwise it will return the current value.

e.attr(name[, value])

A getter/setter for setAttribute. If two arguments are provided, this will call setAttribute (returning this for chaining). If only one argument is provided, it will return the value of getAttribtue.

License

MIT, see LICENSE.md for details.

Current Tags

  • 1.1.8                                ...           latest (6 years ago)

9 Versions

  • 1.1.8                                ...           6 years ago
  • 1.1.7                                ...           6 years ago
  • 1.1.6                                ...           6 years ago
  • 1.1.4                                ...           6 years ago
  • 1.1.3                                ...           6 years ago
  • 1.1.2                                ...           6 years ago
  • 1.1.1                                ...           6 years ago
  • 1.0.0                                ...           6 years ago
  • 0.0.0                                ...           6 years ago
Maintainers (1)
Downloads
Today 0
This Week 0
This Month 1
Last Day 0
Last Week 1
Last Month 1
Dependencies (6)
Dev Dependencies (9)
Dependents (0)
None

Copyright 2014 - 2017 © taobao.org |