Starting with an inexplicable error-about dom node creation in jquery and jquerydom

Source: Internet
Author: User
Tags closing tag

Starting with an inexplicable error-about dom node creation in jquery and jquerydom

There is a string like this: var s = '

How can I get the content value through jquery?

I think this problem is quite simple. It can be obtained through regular expressions or by converting this string into a jq object through jquery $. However, this method has brought about problems.

In my understanding, the method for locating content should be $ (s). find ("meta"). attr ("content"), but it is not correct to obtain the result.

So I made a simple attempt:

var s = '

The obtained result is not a detailed result, but a meta tag.

This is a bit strange. It is probably because var s = '<div>

The result is not what we think, but the html Tag is still removed, but because a div is applied to make $ (s ). find ("meta "). attr ("content") does not protect html tags because it finds the node correctly.

I believe that only the source code of jq can answer this question. I wrote a few lines of simple test code and explained the jq processing mechanism through the breakpoint:

(function() {        debugger;        $('

Facts have proved that the last two methods can obtain results correctly except for the first method. By studying the Code created for the jq object in the jq source code, we finally figured out the cause.

Jq performs a regular expression on the input string:

var parsed = rsingleTag.exec( data );//rsingleTag=/^<(\w+)\s*\/?>(?:<\/\1>|)$/

Let's explain this regular expression:

^ <(\ W +) \ s *\/?
Start with <, followed by at least one character and any blank character, followed by 0 or 1 time/
>
This is not to mention. The preceding values add up to <div> and <meta/>.
(? : <\/\ 1> |) $
Can match <, A/, or blank and end with it
These are the </div> or values following the <br/> self-closing tag.

In this way, if there are no attributes and sub-node strings (such as '

If (parsed) {return [context. createElement (parsed [1])];} // context is the context

If the node string is not passed, a div node is created to place the string into innerHTML of the div:

Tmp = tmp | safe. appendChild (context. createElement ("div"); // create divdom node tmp. innerHTML = wrap [1] + elem. replace (rxhtmlTag, "<$1> </$2>") + wrap [2]; // place the string in innerHTML of the div object

The browser does not allow the div to directly include

After html is filtered out, the string is changed:

Jq searches for the head and end of a node from both ends and closes the tag here as the meta tag.

This is all the conclusions obtained this time. To sum up:

If your tag has no subnode or attribute, jq will create the node directly in the context you passed in after regular expression determination.
If your tag contains subnodes or attributes, the jq regular expression will not work, and a div will be created to pass your string as innerHTML of the div, traverse the attributes and nodes of the dom node inside the div, obtain the class, id, and so on, and then create the real node.
However, the browser does not allow any non-frame Elements to contain html tags and filter them out.
After the html Tag is filtered out, the head tag and the body tag become two tags, and jq searches for the package tag pairs from the external, so these two tags are not identified, the meta tag is a self-closed tag, and is also the outermost closed tag wrapped from the outside to the inside. Therefore, this tag is obtained.
This is probably the case.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.