Javascript 将字符串转换为HTML标记并分配到google map api
我有以下变量:Javascript 将字符串转换为HTML标记并分配到google map api,javascript,html,google-maps,Javascript,Html,Google Maps,我有以下变量: var htmlInput ="<input id="userloc" class="form-control" type="text" placeholder="Search your location">" 我知道Google search box只接受HTML元素,所以我必须将htmlInput字符串转换为HTML标记格式,格式必须与上面所示完全相同。我怎样才能做到这一点?我尝试了$.parseHTML()函数,但没有成功 您只需要查询HTML元素。在本例
var htmlInput ="<input id="userloc" class="form-control" type="text" placeholder="Search your location">"
我知道Google search box只接受HTML元素,所以我必须将htmlInput字符串转换为HTML标记格式,格式必须与上面所示完全相同。我怎样才能做到这一点?我尝试了$.parseHTML()函数,但没有成功 您只需要查询HTML元素。在本例中,您可以像这样获得目标DOM元素
var htmlInput = document.getElementById("userLoc");
var searchBox = new google.maps.places.SearchBox(htmlInput);
请正确参考文档U可以使用jQuery或javascript将html转换为DOM对象
//usingjQuery
var htmlInput = $('<input id="userloc" class="form-control" type="text" placeholder="Search your location" />');
var searchBox = new google.maps.places.SearchBox(htmlInput);
//使用jQuery
var htmlInput=$('');
var searchBox=newgoogle.maps.places.searchBox(htmlInput);
为什么不这样获取元素:var htmlInput=document.getElementById('userloc')代码>
//usingjQuery
var htmlInput = $('<input id="userloc" class="form-control" type="text" placeholder="Search your location" />');
var searchBox = new google.maps.places.SearchBox(htmlInput);