Using The jQuery Migrate Plugin
jQuery( html [, ownerDocument ] )Returns: jQuery
Description: Creates DOM elements on the fly from the provided string of raw HTML.
version added: 1.0jQuery( html [, ownerDocument ] )
- htmlType: htmlStringA string of HTML to create on the fly. Note that this parses HTML, not XML.
- ownerDocumentType: documentA document in which the new elements will be created.
version added: 1.4jQuery( html, attributes )
- htmlType: htmlStringA string defining a single, standalone, HTML element (e.g. <div/> or <div></div>).
- attributesType: PlainObjectAn object of attributes, events, and methods to call on the newly-created element.
Creating New Elements
If a string is passed as the parameter to $()
, jQuery examines the string to see if it looks like HTML (i.e., it starts with <tag ... >
). If not, the string is interpreted as a selector expression, as explained above. But if the string appears to be an HTML snippet, jQuery attempts to create new DOM elements as described by the HTML. Then a jQuery object is created and returned that refers to these elements. You can perform any of the usual jQuery methods on this object:
1
|
$( "<p id='test'>My <em>new</em> text</p>" ).appendTo( "body" ); |
For explicit parsing of a string to HTML, use the $.parseHTML() method.
By default, elements are created with an .ownerDocument
matching the document into which the jQuery library was loaded. Elements being injected into a different document should be created using that document, e.g., $("<p>hello iframe</p>", $("#myiframe").prop("contentWindow").document)
.
If the HTML is more complex than a single tag without attributes, as it is in the above example, the actual creation of the elements is handled by the browser's .innerHTML
mechanism. In most cases, jQuery creates a new <div>
element and sets the innerHTML
property of the element to the HTML snippet that was passed in. When the parameter has a single tag (with optional closing tag or quick-closing) — $( "<img />" )
or $( "<img>" )
, $( "<a></a>" )
or $( "<a>" )
— jQuery creates the element using the native JavaScript .createElement()
function.
When passing in complex HTML, some browsers may not generate a DOM that exactly replicates the HTML source provided. As mentioned, jQuery uses the browser's .innerHTML
property to parse the passed HTML and insert it into the current document. During this process, some browsers filter out certain elements such as <html>
, <title>
, or <head>
elements. As a result, the elements inserted may not be representative of the original string passed.
Filtering isn't, however, limited to these tags. For example, Internet Explorer prior to version 8 will also convert all href
properties on links to absolute URLs, and Internet Explorer prior to version 9 will not correctly handle HTML5 elements without the addition of a separate compatibility layer.
To ensure cross-platform compatibility, the snippet must be well-formed. Tags that can contain other elements should be paired with a closing tag:
1
|
$( "<a href='https://jquery.com'></a>" ); |
Tags that cannot contain elements may be quick-closed or not:
1
2
|
$( "<img>" ); $( "<input>" ); |
When passing HTML to jQuery()
, note that text nodes are not treated as DOM elements. With the exception of a few methods (such as .content()
), they are generally ignored or removed. E.g:
1
2
|
var el = $( "<br>2<br>3" ); // returns [<br>, "2", <br>] el = $( "<br>2<br>3 >" ); // returns [<br>, "2", <br>, "3 >"] |
This behavior is expected. As of jQuery 1.9.0 (and unless using the jQuery Migrate plugin), jQuery()
requires the HTML string to start with a <
(i.e text nodes cannot appear at the front of the HTML string).
As of jQuery 1.4, the second argument to jQuery()
can accept a plain object consisting of a superset of the properties that can be passed to the .attr() method.
Important: If the second argument is passed, the HTML string in the first argument must represent a simple element with no attributes. As of jQuery 1.4, any event type can be passed in, and the following jQuery methods can be called: val, css, html, text, data, width, height, or offset.
As of jQuery 1.8, any jQuery instance method (a method of jQuery.fn
) can be used as a property of the object passed to the second parameter:
1
2
3
4
5
6
7
8
|
$( "<div></div>", { "class": "my-div", on: { touchstart: function( event ) { // Do something } } }).appendTo( "body" ); |
The name "class"
must be quoted in the object since it is a JavaScript reserved word, and "className"
cannot be used since it refers to the DOM property, not the attribute.
While the second argument is convenient, its flexibility can lead to unintended consequences (e.g. $( "<input>", {size: "4"} )
calling the .size()
method instead of setting the size attribute). The previous code block could thus be written instead as:
1
2
3
4
5
6
7
8
|
$( "<div></div>" ) .addClass( "my-div" ) .on({ touchstart: function( event ) { // Do something } }) .appendTo( "body" ); |
Examples:
Create a div element (and all of its contents) dynamically and append it to the body element. Internally, an element is created and its innerHTML property set to the given markup.
1
|
$( "<div><p>Hello</p></div>" ).appendTo( "body" ) |
Create some DOM elements.
1
2
3
4
5
6
7
8
|
$( "<div/>", { "class": "test", text: "Click me!", click: function() { $( this ).toggleClass( "test" ); } }) .appendTo( "body" ); |
Using The jQuery Migrate Plugin的更多相关文章
- 30个非常流行的提示信息插件(jQuery Tooltip Plugin)
在网站的设计中,提示信息是非常细微的功能,但是起着非常重要的作用.如果你的网站中提示信息做的比较好,会给浏览者留下非常深刻的印象,同时也会起到非常好的网站宣传效果,下面介绍了30个比较流行提示信息插件 ...
- JQuery多媒体插件jQuery Media Plugin使用详解
malsup jquery media plugin 该插件可以播放多种类型的多媒体文件包括:Flash, Quicktime, Windows Media Player, Real Player, ...
- jQuery DataTables Plugin Meets C#
Over the weekend, I was doing some work on the internal CMS we use over at eagleenvision.net and I w ...
- jQuery Validation Plugin学习
http://blog.csdn.net/violet_day/article/details/14109261 jQuery Validation Plugin Demo 一.默认校验规则 (1)r ...
- (转)jQuery Validation Plugin客户端表单证验插件
jQuery Validation Plugin客户端表单验证插件 官方文档:http://jqueryvalidation.org/documentation/ 官方demo:http://jque ...
- 表单验证的validate.js插件---jQuery Validation Plugin
早上在公交车上看了一个关于慕课网的教程<表单验证的validate.js插件---jQuery Validation Plugin>,正好可以用到自己近期开发简易微博的注册页面和登录页面, ...
- jQuery validator plugin 之 custom methods 案例1:multi email
1.add method jQuery.validator.addMethod( "multiemail", function (value, element) { var ema ...
- jQuery webcam plugin
jQuery webcam plugin The jQuery webcam plugin is a transparent layer to communicate with a camera di ...
- http://www.vaikan.com/docs/jquery.form.plugin/jquery.form.plugin.html#getting-started
http://www.vaikan.com/docs/jquery.form.plugin/jquery.form.plugin.html#getting-started Jquery.Form 异步 ...
随机推荐
- IconFont使用指南
[IconFont使用指南] 为了使用IconFont,需要先建立自己的项目. 在IconFont.cn中寻找自己喜欢的图标,加入到这个新建的项目. IconFont有三种使用方式,其中FontCla ...
- Java volatile关键字的用法
volatile不能解决同步问题 如果想要理解volatile关键字的作用不得不先了解Java内存模型 摘抄一下来自百度百科的话 在本次线程内,当读取一个变量时,为提高存取速度,编译器优化时有时会先把 ...
- Numpy:ndarray数据类型和运算
Numpy的ndarray:一种多维数组对象 N维数组对象,该对象是一个快速而灵活的大数据集容器,nadarry是一个通用的同构数据多维容器,也就是说,其中的所有元素必须是相同类型的.每个数组都有一个 ...
- Hadoop特点
一:HDFS 1.HDFS上传数据,会将文件切分成指定大小的数据块,并以多副本的数据块存储在机器上. 2. part0是指 副本有2个而且1,2有两个副本 二.YARN 1.负责整个集群的管理和调度 ...
- roof
roof - 必应词典 美[ruf]英[ruːf] n.屋顶:车顶:顶部:有…顶的 v.给…盖顶:盖上屋顶 网络房顶:楼顶:屋脊 变形复数:roofs:过去分词:roofed:现在分词:roofing ...
- Mac下Chrome浏览器的手机模拟器,开启模拟定位
项目接入百度地图,浏览器调试时需要获取定位信息. 1 打开设置->高级->内容设置->位置,将定位设置为“使用前先询问”,并清空禁止列表. 2 审查元素->Network-&g ...
- crm作业知识点集合[二]
知识点1 前面我们实现了这个功能,就是在models中如果有了choice选项,我们可以实现在页面显示这个chocice的value值,而不是key值,我们这个知识点就是在优化一下这个点 首先如果表中 ...
- 20165315 预备作业3 Linux安装及学习
20165315 预备作业3 Linux安装及学习 一.在自己笔记本上安装Linux操作系统 因为对操作电脑的不熟悉,我在第一项任务上就花费了一定的时间,在安装过程有如下问题: 我的电脑是苹果公司的M ...
- AngularJS——第4章 数据绑定
第4章 数据绑定 AngularJS是以数据做为驱动的MVC框架,所有模型(Model)里的数据经由控制器(Controller)展示到视图(View)中. 所谓数据绑定指的就是将模型(Model)中 ...
- linux命令学习之:cp
cp命令用来复制文件或者目录,是Linux系统中最常用的命令之一. 如同时指定两个以上的文件或目录,且最后的目的地是一个已经存在的目录,则它会把前面指定的所有文件或目录复制到此目录中.若同时指定多个文 ...