jQuery object and DOM Element
They're both objects but DOMElements are special objects. jQuery just wraps DOMElements in a Javascript object.
jQuery
returns an element it shows up as [object Object] in an alert.
relationship between jQuery object and DOM element:
A jQuery object is an array-like object that contains DOM element(s). A jQuery object can contain multiple DOM elements depending on the selector you use.
what methods can operate on jQuery object vs DOM element?
jQuery functions (a full list is on the website) operate on jQuery objects and not on DOM elements. You can access the DOM elements inside a jQuery function using .get() or accessing the element at the desired index directly:
$("selector")[] // Accesses the first DOM element in this jQuery object
$("selector").get() // Equivalent to the code above
$("selector").get() // Retrieve a true array of DOM elements matched by this selector
In other words, the following should get you the same result:
<div id="foo"></div>
alert($("#foo")[0]);
alert($("#foo").get(0));
alert(document.getElementById("foo"));
DOMElements
When getElementByID returns an element it shows up as [object HTMLDivElement].
Question
1.How can I convert a JavaScript DOM object to a jQuery object?Is there any way to convert a js DOM object to a jQuery object or using the this keyword in jQuery?
<tr onclick="changeStatus(this)">
function changeStatus(myObject) {
XXX.removeClass();
}
answer:
var $this = $(myObject);
$this is a jQuery object. You can create jQuery objects from DOM elements.
<tr onclick="changeStatus(this)">
function changeStatus(myObject) {
$(myObject).removeClass();
}
I would like to recommend doing your event binding with jQuery as well:
This is nice because now all the JS code is in one place and can be updated (in my opinion) more easily. Note that the class I added to the <tr> element is not necessary if you want to bind to all <tr> elements in the DOM.
<tr class="change-status">
$('.change-status').on('click', function () {
$(this).removeClass( ... );
});
2.My first question is if this is a DOM object or a jQuery Object?
I would assume that it is a DOM object as we are passing it through a jQuery modifier in the next statement i.e $(this).find(".tool-name") to further extract data from it. Is that correct?
$(".foo").click(function() {
var displayTool = $(this).find(".tool-name").text() //is this a jquery object or DOM object ?
});
answer:
Yes, this is always a DOM object within a jQuery click handler.
As to why this is...
Do you always want a jQuery object containing the clicked element within a click handler? No, there are occasions when you do not. For example, say you wanted to remove another element from the page when an element was clicked:
$('.delete').click(function() {
$('.spinner').remove();
});
You never use this.
Or you may want simply to do something with the element's id:
$('.delete').click(function() {
console.log(this.id);
});
In neither case do you want to have a jQuery selection. And since building a jQuery selection is a relatively expensive operation, it is much better not to create it unless you explicitly say you want it by doing $(this).
You might be thinking "ah, but we already built the selection in the original line of code":
$(".foo").click(function() {
.foo means something different. It means "all the elements with the class foo", not "the element that was just clicked".
jQuery object and DOM Element的更多相关文章
- JQuery Object vs. DOM element
JQuery Object 和 DOM的区别 HTML DOM 定义了访问和操作HTML文档的标准方法.其中 document 是DOM 树的根对象 ,在浏览器宿主环境中,可以通过JS操作HTML D ...
- How do I pull a native DOM element from a jQuery object? | jQuery Learning Center
How do I pull a native DOM element from a jQuery object? | jQuery Learning Center How do I pull a na ...
- jQuery核心之jQuery Object及其相关的常用方法
1.jQuery Object 和 原生的DOM对象之间有许多方法是不一样的,用jQuery的方法大部分情况下返回的是jQuery Object,但是jQuery也提供了一些方法可以很轻松的获取原生的 ...
- 惊叹jQuery(解决jQuery对象到DOM的转换)
jQuery是一个javascript框架,但绝对不是通常意义上的一些包装,个人感觉是一个改变js控制方式的框架.我们可以像美工通过写css分离页面代码一样,通过jQuery来分离页面与效果..下面转 ...
- jQuery中的DOM操作总结
jQuery中的DOM操作 DOM是Document Object Medel的缩写,它的意思是文档对象模型,根据W3C的官方说法,DOM是一种跟浏览器,平台以及语言都没有关系的一种规范,也就是一种接 ...
- jQuery基础教程-第8章-002Adding jQuery object methods
一.Object method context 1.We have seen that adding global functions requires extending the jQuery ob ...
- jQuery对象与dom对象的转换
一直以来对于通过jQuery方式获取的对象使不能直接使用JavaScript的一些方法的,开始的时候不理解,现在此案知道,原来jQuery获得的对象并不和我们平时使用getElementById获得的 ...
- Jquery基础之DOM操作
转自:http://www.cnblogs.com/bro-ma/p/3063942.html JQuery中的DOM操作主要对包括:建[新建].增[添加].删[删除].改[修改].查[查找][像数据 ...
- jQuery对象和DOM对象的相关知识
所谓的DOM就是Document Object Model(文档对象模型)的缩写,或许是我水平低的缘故,感觉就是HTML的标记元素嘛,所以作者画了下面的图: 像这样的元素,就是所谓的DOM对象,获取值 ...
随机推荐
- BUG笔记:Firefox select选项右侧边框没了
Firefox 的default select在某些情况下右侧边框会消失.截图如下: 这个目前为止没有看到有任何解决方案,HACK也没有...囧... 有高人知道吗?
- python string 模块
标准库 python3 python2.7 都可以用 sting.ascii_letters是生成所有字母,从a-z和A-Z, string.digits是生成所有数字0-9. import stri ...
- Java编程实现获取本机IP和计算机名的功能
版权声明:本文为博主原创文章.未经博主同意不得转载. https://blog.csdn.net/wangshuxuncom/article/details/35990847 import java. ...
- HTTP头部信息解释分析(详细整理)
这篇文章为大家介绍了HTTP头部信息,中英文对比分析,还是比较全面的,若大家在使用过程中遇到不了解的,可以适当参考下 HTTP 头部解释 1. Accept:告诉WEB服务器自己接受什么介质类型,*/ ...
- 使用vue如何默认选中单选框
使用了vue以后,发现这真的是一个灵活高效的框架,能够轻松实现页面的实时刷新.那么,今天先聊聊单选框的使用.一般我们使用单选框,会这么写: //HTML <input type=" c ...
- javascript日期操作详解
时间对象是一个我们经常要用到的对象,无论是做时间输出.时间判断等操作时都与这个对象离不开.除开JavaScript中的时间对象外,在VbScript中也有许多的时间对象,而且非常好用.下面还是按照我们 ...
- Ubuntu单用户模式(安全模式)
说下我遇到的情况,ubuntu服务器,防火墙关闭,连的外网.服务器中毒,病毒自动生成用户,然后病毒进程开启启动,进程启动后,cpu立马占满,服务器立马卡死,本想着服务器启动后通过top命 ...
- (转)HTML5开发中Access-Control-Allow-Origin跨域问题
今天准备通过JavaScript的方式调用问说问答的内容,由于使用的不同的二级域名,遇到了一个跨域问题,虽然可以使用JSON或者XML来解决这个问题,但是我们可以通过Access-Control-Al ...
- soapUI-Conditional Goto
1.1.1 Conditional Goto 1.1.1.1 概述 - Conditional Goto Conditional Goto TestStep包含任意数量的XPath/JSONPath ...
- [LeetCode] 198. House Robber _Easy tag: Dynamic Programming
You are a professional robber planning to rob houses along a street. Each house has a certain amount ...