简体   繁体   English

如何将事件侦听器添加到页面加载后不会立即显示的元素?

[英]How can I add an event listener to an element that does not show up immediately on page load?

I am working with Google Maps API and I am trying to get a website running that shows you information about a specific location when it is clicked on. 我正在使用Google Maps API,正在尝试运行一个网站,该网站在单击时会向您显示有关特定位置的信息。 There are little clickable buttons at the bottom of the information div that are meant to change the information displayed based on the button that is clicked on. 信息div底部几乎没有可单击的按钮,这些按钮可用来更改基于所单击按钮的显示信息。

http://i61.tinypic.com/vxge4o.jpg -->example http://i61.tinypic.com/vxge4o.jpg- >示例

These buttons only show up after the location on the map is clicked on, so my addEventListener shows up as an error "Cannot call method 'addEventListener' of null"; 这些按钮仅在单击地图上的位置后显示,因此我的addEventListener显示为错误“无法调用方法'addEventListener'为null”。 I'm assuming this occurs because the method is looking for a button that isn't there yet. 我假设发生这种情况是因为该方法正在寻找尚不存在的按钮。

Is there a way to get the addEventListener method working only after a location has been selected on the map? 是否只有在地图上选择位置后才能使addEventListener方法起作用?

Here is the Javascript code I currently have: 这是我目前拥有的Javascript代码:

function change() {
  var within = document.getElementById("information").innerhtml;
  within = "<p>hi</p>";
}

function recreate(){
  var alter = document.getElementById("2");
  alter.addEventListener('click', function(event) {
    change();
  });
}

And here is the HTML snippet that corresponds with it: 以下是与此相对应的HTML代码段:

 <div id="information">
    <p>Location</p>
    <div id="details">
        <p>Candidate Type:</p>
        <p>Candidate Name:</p>
        <p>Location Name:</p>
        <p>PeopleSoft Location Code:</p>
        <p>Street Address:</p>
        <p>City:</p>
        <p>County:</p>
        <p>State:</p>
        <p>Zipcode:</p>
        <p>Latitude:</p>
        <p>Longitude:</p>
        <p>From 2C Survey?</p>
    </div>
    <center>
        <input type="image" src="*image source*" id="1"/>
        <input type="image" src="*image source*" id="2"/>
        <input type="image" src="*image source*" id="3"/>
        <input type="image" src="*image source*" id="4"/>
        <input type="image" src="*image source*" id="5"/>
        <input type="image" src="*image source*" id="6"/>
        </center>
    <div id="back"><center>
        <input type="image" src="*image source*"/>
    </center></div>
</div>

I have attempted the solutions on each of these pages without avail. 我在这些页面的每一个上都尝试了解决方案,但无济于事。

https://stackoverflow.com/a/9856159/3838411 https://stackoverflow.com/a/9856159/3838411

Attach a body onload event with JS 使用JS附加body onload事件

When I try to use the window.onload option, my map breaks and just shows a gray box. 当我尝试使用window.onload选项时,地图破裂,仅显示一个灰色框。 I tried putting it in a script tag in the html file as well, but that did not work either. 我也尝试将其放在html文件的script标记中,但这也不起作用。

I am sure there is a way of doing this, but I am quite new to JavaScript so I am probably missing some idea here. 我敢肯定有一种方法可以做到这一点,但是我对JavaScript还是很陌生,所以我可能在这里缺少一些想法。 Any help would be great! 任何帮助将是巨大的!

EDIT 编辑

I know this may not be the easiest, but since I am learning JavaScript right now, I would like an answer in pure JavaScript, although I know jQuery would probably be more simple. 我知道这可能不是最简单的,但是由于我现在正在学习JavaScript,所以我想用纯JavaScript给出答案,尽管我知道jQuery可能会更简单。 Thanks again! 再次感谢!

Second Edit 第二次编辑

My original information div is already created in the HTML file. 我的原始信息div已在HTML文件中创建。 I used CSS to hide it 我用CSS隐藏了它

#information{
display: none;
}

and then used: 然后使用:

google.maps.event.addListener(marker, 'click', function(){
    map.setZoom(16);
    map.setCenter(marker.getPosition());
    document.getElementById("information").style.display="block";
});

to display it when the point is clicked on 单击该点时显示它

If you can re-purpose the elements rather than creating new ones for each use, you can try something like this (rough example to so you the idea): JSFiddle 如果您可以重新设计元素的用途,而不是每次使用都创建新元素,则可以尝试这样的事情(举一个粗糙的例子,这样您就可以了): JSFiddle

//traverse the DOM to get to your center container
var thediv = document.getElementById("information");
var thecenter = thediv.getElementsByTagName("input");

//set up the event handler for all of the images with a loop
for (i = 0; i < thecenter.length; i++) { 
    thecenter[i].addEventListener("click", function(){ inputimageclicked(this) }, false);
}

//function to handle all image clicks
//as you can see, you can access the object that fired the 
//click event and use it to vary the effect of the function
//you can access any attribute, style, etc of the target object
function inputimageclicked(obj) { 
   alert("input image with id: " + obj.id); 
   //this is where you will determine what <input> fire the event 
   //and react as you need to (call another function, show/hide 
   //something, etc.
}

*You will need to handle old IE attachEvent alternatives if you need to support those older browsers *如果您需要支持那些旧的IE浏览器,则需要处理旧的IE AttachEvent替代方法

I actually used @epipav 's suggestion of making the event occur after the elements were shown. 实际上,我使用@epipav的建议来使事件在元素显示之后发生。 It ended up looking like this. 最终看起来像这样。

google.maps.event.addListener(marker, 'click', function(){
    *actions that are put into place*

    var myBtn = document.getElementById("2");
    myBtn.addEventListener('click', function(event) {
      change1();
    }, false);
    function change1(){ 
      *some function*
    };
}, false);

I'm not sure if I should be editing my previous post or creating an answer, since this is the solution I used. 我不确定是应该编辑上一篇文章还是创建答案,因为这是我使用的解决方案。 I'm also not sure if I should delete the question or not, as it may help others in the future. 我也不确定是否应该删除该问题,因为这将来可能会帮助其他人。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM