返回值:jQueryjQuery(html, props)

概述

根据提供的原始 HTML 标记字符串,动态创建由 jQuery 对象包装的 DOM 元素。同时设置一系列的属性、事件等。

参数

htmlString

用于动态创建DOM元素的HTML标记字符串

propsMap

用于附加到新创建元素上的属性、事件和方法

示例

描述:

动态创建一个 div 元素(以及其中的所有内容),并将它追加到 body 元素中。在这个函数的内部,是通过临时创建一个元素,并将这个元素的 innerHTML 属性设置为给定的标记字符串,来实现标记到 DOM 元素转换的。所以,这个函数既有灵活性,也有局限性。

jQuery 代码:
$("<div>", {
  "class": "test",
  text: "Click me!",
  click: function(){
    $(this).toggleClass("test");
  }
}).appendTo("body");

描述:

创建一个 <input> 元素,同时设定 type 属性、属性值,以及一些事件。

jQuery 代码:
$("<input>", {
  type: "text",
  val: "Test",
  focusin: function() {
    $(this).addClass("active");
  },
  focusout: function() {
    $(this).removeClass("active");
  }
}).appendTo("form");

目录 索引

输入要查找的关键字:

#id*.class:animated:button:checkbox:checked:contains:disabled:empty:enabled:eq:even:file:first-child:first:gt:has:header:hidden:hidden:image:input:last-child:last:lt:not:nth-child:odd:only-child:parent:password:radio:reset:selected:submit:text:visible[attrSel1][attrSel2][attrSelN][attribute!=value][attribute$=value][attribute*=value][attribute=value][attribute][attributeadd(expr, [context])addClass(class)addClass(function(index, class))after(content)after(function)ajaxComplete(callback)ajaxError(callback)ajaxSend(callback)ajaxStart(callback)ajaxStop(callback)ajaxSuccess(callback)ancestor descendantandSelf()animate(param,[dur],[e],[fn])animate(params, options)append(content)append(function(index, html))appendTo(content)attr(key, fn)attr(key, value)attr(name)attr(properties)before(content)before(function)bind(type, [data], fn)blur()blur(fn)change()change(fn)children([expr])clearQueue([queueName])click()click(fn)clone()clone(true)closest([expr])contents()contextcss(name)css(name, function(index, value))css(name, value)css(properties)data([name])data(name, value)data(obj)dblclick()dblclick(fn)delay(duration, [queueName])dequeue(name)detach([expr])die([type], [fn])each(callback)elementempty()end()eq(index)error()error(fn)fadeIn(speed, [callback])fadeOut(speed, [callback])fadeTo(speed, opacity, [fn])filter(expr)filter(fn)find(expr)first()focus()focus(fn)focusin( [fn] )focusout( [fn] )get()get(index)has(expr)hasClass(class)height()height(val)hide()hide(speed, [callback])hover(over, out)html()html(function(index, html))html(val)index(subject)innerHeight()innerWidth()insertAfter(content)insertBefore(content)is(expr)jQuery()jQuery(callback)jQuery(elements)jQuery(expr, [context])jQuery(html, [ownerDoc])jQuery(html, props)jQuery.ajax([options])jQuery.ajaxSetup([options])jQuery.boxModeljQuery.browser.versionjQuery.browserjQuery.contains(container, contained)jQuery.data([element], [key])jQuery.data(element, key, val)jQuery.each(object, [callback])jQuery.error(message)jQuery.extend([d],tgt,obj1,[objN])jQuery.extend(object)jQuery.fn.extend(object)jQuery.fx.offjQuery.get(url, [data], [fn], [type])jQuery.getJSON(url, [data], [fn])jQuery.getScript(url, [callback])jQuery.grep(array, fn, [invert])jQuery.inArray(value, array)jQuery.isArray(obj)jQuery.isEmptyObject(obj)jQuery.isFunction(obj)jQuery.isPlainObject(obj)jQuery.makeArray(obj)jQuery.map(array, callback)jQuery.merge(first, second)jQuery.noConflict()jQuery.noConflict(extreme)jQuery.noopjQuery.param(obj, [traditional])jQuery.parseJSON(json)jQuery.post(url, [data], [fn], [type])jQuery.proxy(function, scope)jQuery.supportjQuery.toArray()jQuery.trim(str)jQuery.unique(array)keydown()keydown(fn)keypress()keypress(fn)keyup()keyup(fn)last()lengthlive(type, [data], fn)load(url, [data], [callback])map(callback)mousedown(fn)mousemove(fn)mouseout(fn)mouseover(fn)mouseup(fn)next([expr])nextAll([expr])nextUntil([expr])not(expr)offset(coordinates)offsetParent()offsetone(type, [data], fn)outerHeight(options)outerWidth(options)parent > childparent([expr])parents([expr])parentsUntil([expr])position()prepend(content)prepend(function(index, html))prependTo(content)prev + nextprev ~ siblingsprev([expr])prevAll([expr])prevUntil([expr])queue(name ,callback)queue(name)queue(name, queue)ready(fn)remove([expr])removeAttr(name)removeClass(class | fn])removeClass(class)removeData(name)replaceAll(selector)replaceWith(content)resize(fn)scroll(fn)scrollLeft()scrollLeft(val)scrollTop()scrollTop(val)select()select(fn)selector1,selector2,selectorNselectorserialize()serializeArray()show()show(speed, [callback])siblings([expr])size()slice(start, [end])slideDown(speed, [callback])slideToggle(speed, [callback])slideUp(speed, [callback])stop([clearQueue], [gotoEnd])submit()submit(fn)text()text(function(index, text))text(val)toggle()toggle(fn, fn2, [fn3, fn4, ...])toggle(speed, [callback])toggle(switch)toggleClass(class)toggleClass(class, switch)toggleClass(function(index, class), [switch])trigger(type, [data])triggerHandler(type, [data])unbind([type], [data])unload(fn)unwrap()val()val(array)val(function(index, value))val(val)width()width(val)wrap(elem)wrap(fn)wrap(html)wrapAll(elem)wrapAll(html)wrapInner(elem)wrapInner(fn)wrapInner(html)
#id*.class:animated:button:checkbox:checked:contains:disabled:empty:enabled:eq:even:file:first-child:first:gt:has:header:hidden:hidden:image:input:last-child:last:lt:not:nth-child:odd:only-child:parent:password:radio:reset:selected:submit:text:visible[attrSel1][attrSel2][attrSelN][attribute!=value][attribute$=value][attribute*=value][attribute=value][attribute][attributeadd(expr, [context])addClass(class)addClass(function(index, class))after(content)after(function)ajaxComplete(callback)ajaxError(callback)ajaxSend(callback)ajaxStart(callback)ajaxStop(callback)ajaxSuccess(callback)ancestor descendantandSelf()animate(param,[dur],[e],[fn])animate(params, options)append(content)append(function(index, html))appendTo(content)attr(key, fn)attr(key, value)attr(name)attr(properties)before(content)before(function)bind(type, [data], fn)blur()blur(fn)change()change(fn)children([expr])clearQueue([queueName])click()click(fn)clone()clone(true)closest([expr])contents()contextcss(name)css(name, function(index, value))css(name, value)css(properties)data([name])data(name, value)data(obj)dblclick()dblclick(fn)delay(duration, [queueName])dequeue(name)detach([expr])die([type], [fn])each(callback)elementempty()end()eq(index)error()error(fn)fadeIn(speed, [callback])fadeOut(speed, [callback])fadeTo(speed, opacity, [fn])filter(expr)filter(fn)find(expr)first()focus()focus(fn)focusin( [fn] )focusout( [fn] )get()get(index)has(expr)hasClass(class)height()height(val)hide()hide(speed, [callback])hover(over, out)html()html(function(index, html))html(val)index(subject)innerHeight()innerWidth()insertAfter(content)insertBefore(content)is(expr)jQuery()jQuery(callback)jQuery(elements)jQuery(expr, [context])jQuery(html, [ownerDoc])jQuery(html, props)jQuery.ajax([options])jQuery.ajaxSetup([options])jQuery.boxModeljQuery.browser.versionjQuery.browserjQuery.contains(container, contained)jQuery.data([element], [key])jQuery.data(element, key, val)jQuery.each(object, [callback])jQuery.error(message)jQuery.extend([d],tgt,obj1,[objN])jQuery.extend(object)jQuery.fn.extend(object)jQuery.fx.offjQuery.get(url, [data], [fn], [type])jQuery.getJSON(url, [data], [fn])jQuery.getScript(url, [callback])jQuery.grep(array, fn, [invert])jQuery.inArray(value, array)jQuery.isArray(obj)jQuery.isEmptyObject(obj)jQuery.isFunction(obj)jQuery.isPlainObject(obj)jQuery.makeArray(obj)jQuery.map(array, callback)jQuery.merge(first, second)jQuery.noConflict()jQuery.noConflict(extreme)jQuery.noopjQuery.param(obj, [traditional])jQuery.parseJSON(json)jQuery.post(url, [data], [fn], [type])jQuery.proxy(function, scope)jQuery.supportjQuery.toArray()jQuery.trim(str)jQuery.unique(array)keydown()keydown(fn)keypress()keypress(fn)keyup()keyup(fn)last()lengthlive(type, [data], fn)load(url, [data], [callback])map(callback)mousedown(fn)mousemove(fn)mouseout(fn)mouseover(fn)mouseup(fn)next([expr])nextAll([expr])nextUntil([expr])not(expr)offset(coordinates)offsetParent()offsetone(type, [data], fn)outerHeight(options)outerWidth(options)parent > childparent([expr])parents([expr])parentsUntil([expr])position()prepend(content)prepend(function(index, html))prependTo(content)prev + nextprev ~ siblingsprev([expr])prevAll([expr])prevUntil([expr])queue(name ,callback)queue(name)queue(name, queue)ready(fn)remove([expr])removeAttr(name)removeClass(class | fn])removeClass(class)removeData(name)replaceAll(selector)replaceWith(content)resize(fn)scroll(fn)scrollLeft()scrollLeft(val)scrollTop()scrollTop(val)select()select(fn)selector1,selector2,selectorNselectorserialize()serializeArray()show()show(speed, [callback])siblings([expr])size()slice(start, [end])slideDown(speed, [callback])slideToggle(speed, [callback])slideUp(speed, [callback])stop([clearQueue], [gotoEnd])submit()submit(fn)text()text(function(index, text))text(val)toggle()toggle(fn, fn2, [fn3, fn4, ...])toggle(speed, [callback])toggle(switch)toggleClass(class)toggleClass(class, switch)toggleClass(function(index, class), [switch])trigger(type, [data])triggerHandler(type, [data])unbind([type], [data])unload(fn)unwrap()val()val(array)val(function(index, value))val(val)width()width(val)wrap(elem)wrap(fn)wrap(html)wrapAll(elem)wrapAll(html)wrapInner(elem)wrapInner(fn)wrapInner(html)