javascript - What are the limitations on an unattached DOM element in JS or JQuery created dynamically? -


I had a problem trying to develop a custom control for Google Maps, in which many examples of using raw javascript Are there. I am trying to customize the code to use JQuery to simplify it. It is important to understand the background of what I am doing.

I have narrowed down the problem to the following small, small segment of code:

  $ (document) .ready (function () {... var jq_outer_div = $ ('& Lt; div id = "outer_div"> '); // $ ('body'). Attachment (jq_outer_div); outer_div = $ ('# outer_div') Error Report shown below. (0); Outer_div.index = 1; // Problem: map.controls [google.maps.ControlPosition.RIGHT_TOP] .push (outer_div); $ ('# outer_div'). Lt; div id = "Control_div" & gt; Home & lt; / div & gt; '); ...});  

I am trying to get simple JS elements from JQuery objects (as posted on many questions in this forum) and then specify an index. For example, see:.

  Uncategorized typed error: undefined  

can not set properties 'index' when I attach to "jq_outer_div", for the element "body" element , It works, I commented on that line to highlight this question. Question: Can any element be made as a jactoic object, then remove the pure non-zinc element and then add this element without adding the properties anywhere in the DOM?

Is this work (the element is not attached anywhere, or is it?):

  var myElement = document.createElement ('div'); MyElement.style.backgroundColor = 'white';  

Then why did I get an error when specifying the value for the index attribute of an unchanged element?

The error you are receiving is because you are trying to get an element Which is not on your DOM. If you just do:

  $ (document) .ready (function () {... var jq_outer_div = $ ('& lt; div id = "outer_div">  ; / Div & gt; '); // $ (' body ') attached (jq_outer_div) ;. outer_div = jq_outer_div.get (0); outer_div.index = 1; // report map.controls error shown below [google .maps.ControlPosition.RIGHT_TOP] .push (outer_div); //$ ('#outer_div').append('<div id = "control_div"> home  gt; '); / Adding the Div to the object: jq_outer_div.append ('& lt; div id = "control_div" & gt; Home & lt; / div & gt;'); // Adding an object to the DOM: $ ('body'). (Jq_outer_div); ...  

});

You should not get any errors.


Comments