最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

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

programmeradmin0浏览0评论

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")

发布评论

评论列表(0)

  1. 暂无评论