因此,我在输入地址时使用Google API在下拉列表中显示位置列表。
当我单击下拉菜单中的选项/地址之一时,我希望警报触发并显示“嗨”。最好的方法是什么?
更多信息:
下拉菜单中的每个项目都具有'pac-item'
类,并且位于'pac-container'
的div内:
<div class="pac-container pac-logo" style="width: 601px; position: absolute; left: 15px; top: 70px; display: none;">
<div class="pac-item"><span class="pac-icon pac-icon-marker"></span><span class="pac-item-query"><span class="pac-matched">S</span>ão Paulo</span><span>State of São Paulo, Brazil</span></div>
<div class="pac-item"><span class="pac-icon pac-icon-marker"></span><span class="pac-item-query"><span class="pac-matched">S</span>an Francisco</span><span>CA, USA</span></div>
</div>
所以我一直在尝试通过类名来获取Elements,然后当您单击其中一个元素时,它应该警告“ hi”
但是,由于下拉菜单中的项直到您单击输入后才加载,我认为这会在页面加载和Javascript试图通过其类名获取元素时引起问题。
解决此问题的最佳方法是什么?
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<meta http-equiv="X-UA-Compatible" content="ie=edge" />
<link rel="stylesheet" href="style.css" />
<link
rel="stylesheet"
href="https://stackpath.bootstrapcdn.com/bootstrap/4.2.1/css/bootstrap.min.css"
integrity="sha384-GJzZqFGwb1QTTN6wy59ffF1BuGJpLSa9DkKMp0DgiMDm4iYMj70gZWKYbI706tWS"
crossorigin="anonymous"
/>
<script
src="https://code.jquery.com/jquery-3.3.1.slim.min.js"
integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo"
crossorigin="anonymous"
></script>
<script
src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.6/umd/popper.min.js"
integrity="sha384-wHAiFfRlMFy6i5SRaxvfOCifBUQy1xHdJ/yoi7FRNXMRBu5WHdZYu1hA6ZOblgut"
crossorigin="anonymous"
></script>
<script
src="https://stackpath.bootstrapcdn.com/bootstrap/4.2.1/js/bootstrap.min.js"
integrity="sha384-B0UglyR+jN6CkvvICOB2joaf5I4l3gm9GU6Hc1og6Ls7i6U/mkkaduKaBhlAXv9k"
crossorigin="anonymous"
></script>
<title>Document</title>
</head>
<body>
<div class="col-md-6">
<form id="distance_form">
<div class="form-group"><label>Origin: </label> <input class="form-control" id="from_places" placeholder="Enter a location" />
<input id="origin" name="origin" required="" type="hidden" />
</div>
</form>
</div>
<script>
$(function () {
// add input listeners
google.maps.event.addDomListener(window, "load", function () {
var from_places = new google.maps.places.Autocomplete(
document.getElementById("from_places")
);
});
console.log(document.getElementsByClassName("pac-item"))
let googleDropDown = document.getElementsByClassName("pac-item")
googleDropDown.addEventListener('click', alert)
function alert(){
alert('hi')
}
});
</script>
<script async defer src="https://maps.googleapis.com/maps/api/js?key=YOUR_API_KEY&libraries=places&language=en"></script>
</body>
</html>
编辑:
将其包装在google.maps.event.addDomListener
内可以使我将各项记录到控制台,但也会导致以下错误:
答案 0 :(得分:0)
您可以使用.on()函数确保当前元素和将来的元素将处理您的click事件
$(function () {
// add input listeners
google.maps.event.addDomListener(window, "load", function () {
var from_places = new google.maps.places.Autocomplete(
document.getElementById("from_places")
);
});
$("body").on("click", ".pac-container > .pac-item", function() {
alert('hi');
});
});