开发者

JQuery, Google Maps and $('#myId') == getElementById("myId")

开发者 https://www.devze.com 2023-01-16 20:54 出处:网络
Why this isn\'t true? $(\'#myId\') == document.getElementById(\"myId\") I\'m using JQuery 1.4.2 and trying to insert a GMap into an div element.

Why this isn't true?

$('#myId') == document.getElementById("myId")

I'm using JQuery 1.4.2 and trying to insert a GMap into an div element.

So, this works:

new google.maps.Map开发者_如何转开发(document.getElementById("myId"),myOptions);

but this doesn't

new google.maps.Map($("myId"),myOptions);


You have a couple issues. First, ID selectors use #. Second, $(...) is a jQuery object, and you need to pass a DOM element.

Use $('#myId').get(0)

The get method.


It doesn't work because the google.maps.Map() constructor expects a a DOM element, while the jQuery selector returns a jQuery object.

You may want to use:

new google.maps.Map($("#myId")[0], myOptions);

Further reading:

  • Google Maps API Groups: Why doesn't jQuery selection work?


$('#myId') creates a jQuery selection, while GMap requires a DOM element. You can convert the jQuery selection into a DOM element by using the $().get function:

new google.maps.Map($("#myId").get(0), myOptions);
0

精彩评论

暂无评论...
验证码 换一张
取 消