jquery - Javascript - Convert dom element reference to node - Stack Overflow

in jQuery you can do something like this:var domElement = document.getElementById("myId");va

in jQuery you can do something like this:

var domElement = document.getElementById("myId");
var tmp = jQuery(domElement);

upon which you can get the node

var node = tmp[0];

I would like to know how something like this can be done in native javascript.

Regards

in jQuery you can do something like this:

var domElement = document.getElementById("myId");
var tmp = jQuery(domElement);

upon which you can get the node

var node = tmp[0];

I would like to know how something like this can be done in native javascript.

Regards

Share Improve this question edited Mar 10, 2014 at 8:00 Felix Kling 818k181 gold badges1.1k silver badges1.2k bronze badges asked Mar 10, 2014 at 7:39 user1221121user1221121 1
  • Honest question: What did you think domElement was and what the difference between domElement and node was? Assuming you knew that document.getElementById is the DOM API, not jQuery. – Felix Kling Commented Mar 10, 2014 at 8:01
Add a ment  | 

3 Answers 3

Reset to default 1

Your domElement variable is the node of the DOM tree. That tmp variable wraps it into a jQuery object and the [0] indexer gets the DOM element back. So the native JavaScript is the first line of your code.

var domElement = document.getElementById("myId");

here you get the DOMelement or DOM Node

var tmp = jQuery(domElement);

here you get the DOM element wrapped in jQuery. So its like DOM Element wrapped in jQuery so that jQuery functions can applied to it.

In effect there is no difference between DOM element and node in native javascript.

In short var node = document.getElementById("myId"); is exactly what you want to do in your code with naive javascript.

To explain, in var domElement = document.getElementById("myId");

you are referencing the node with myId to domElement, then with var tmp = jQuery(domElement);, you get that dom element as jquery object.

var node = tmp[0]; selects first child of tmp as dom element which in conclusion equals to document.getElementById("myId")

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745443759a4627950.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信