Home > Web Front-end > JS Tutorial > What is the context attribute? Detailed explanation of context attribute examples

What is the context attribute? Detailed explanation of context attribute examples

伊谢尔伦
Release: 2017-06-19 09:11:00
Original
5008 people have browsed it

Overview

contextAttributesReturns the original DOM node content passed to jQuery(), that is The second parameter of jQuery(). If not specified, context points to the current document (document).

Can be used with selector to accurately detect selector query situations. These two properties are useful to plugin developers.

If the context parameter is not passed in at that time, this parameter defaults to the current document (document). This property belongs to the jQuery object (instance).

Syntax

This property was added in jQuery 1.3, but was marked as obsolete in jQuery 1.10. This attribute is maintained only to support the live() method in the jQuery Migrate plug-in. This attribute may be removed in a future version.

jQueryObject.context

The return value of the context property is of Element type, even if the context parameter passed into the jQuery(selector, context) function is not of Element type. If the context parameter passed in at that time is a jQuery object, the context property of the jQuery object is returned.

Example description:

Take the following HTML code as an example:

<div id="n1">
    <div id="n2">
        <ul id="n3">
            <li id="n4">item1</li>
            <li id="n5">item2</li>
            <li id="n6">item3</li>
        </ul>
    </div>  
</div>
Copy after login

We write the following jQuery code:

var $li = $("ul li");
// 返回当前文档的document对象
document.writeln( $li.context ); // [object HTMLDocument]
document.writeln( $li.context === document ); // true
var n1 = document.getElementById("n1");
var $n3 = $( "#n3", n1 );
// 返回n1
document.writeln( $n3.context ); // [object HTMLDivElement]
document.writeln( $n3.context === n1 ); // true
var $n2 = $("#n2");
var $n4 = $( "#n4", $n2 );
// 返回$n2的context属性:document对象
document.writeln( $n4.context ); // [object HTMLDocument]
document.writeln( $n4.context === document ); // true
var $n3 = $("#n3", n1);
var $n5 = $( "#n5", $n3 );
// 返回$n3的context属性:n1
document.writeln( $n5.context ); // [object HTMLDivElement]
document.writeln( $n5.context === n1 ); // true
Copy after login

Example: Detect the document content used

jQuery code:

 $("ul").append("<li>"+$("ul").context+"</li>")
           .append("<li>"+$("ul", document.body).context.nodeName+"</li>");
Copy after login

Result:

 [object HTMLDocument]//如果是IE浏览器,则返回[object]
   BODY
Copy after login

[Note]: $("ul", document.body).context.nodeName This sentence means to find the ul element in document.body.

The above is the detailed content of What is the context attribute? Detailed explanation of context attribute examples. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template