简体   繁体   English

jQuery-.click在任何地方单击时触发应:在单击链接时

[英]JQuery - .click triggering when clicked anywhere should: upon clicking on a link

https://dl.dropboxusercontent.com/u/63617776/Capture.PNG https://dl.dropboxusercontent.com/u/63617776/Capture.PNG

So as on the image, when you click on the map, a div is changed. 因此,就像在图像上一样,当您单击地图时,div会更改。 Now when I click on a link in the div, I want the google maps div to change to another map. 现在,当我单击div中的链接时,我希望google maps div更改为另一张地图。

But, the code I wrote either doesn't trigger at all or it triggers when I click anywhere on the page. 但是,我编写的代码根本不会触发,或者在我单击页面上的任何位置时都会触发。

$('#nowydwor').ready(function(){
$(this).click(function(){
         alert('foo');
    });
});

Ofcourse the link looks like this: {a id="nowydwor"} text {/a} (for some reason i couldn't enter < so I replaced it with {) 当然,链接看起来像这样:{a id =“ nowydwor”}文本{/ a}(由于某种原因,我无法输入<,所以我将其替换为{)

This triggers when user clicks anywhere on the page, for some reason. 当用户出于某种原因单击页面上的任意位置时,这将触发。 Also this is only a testcode for now, it is meant to display the alert. 同样,这只是目前的测试代码,用于显示警报。 :) Any ideas? :) 有任何想法吗?

EDIT: The link is contained in .html(), in a switch() statement. 编辑:链接包含在.html()中,在switch()语句中。

case '#mazowieckie':
   $('#info').html("CONTENT </h5><hr><strong><a id='nowydwor'>Skład Nowy Dwór Mazowiecki</a></strong> CONTENT");      

break; 打破;

Calling ready only makes sense if you call it on the document / window to get notified as soon as the DOM is ready. 仅当在DOM准备就绪时在document / window上调用它以获取通知时,调用ready才有意义。

Try to bind the click handler on your DOM element directly: 尝试直接在您的DOM元素上绑定click处理程序:

$('#nowydwor').on('click', function(){
  alert('foo');
});

I think what you were trying to do, is assign the click handler after the content of #info has changed. 我认为您正在尝试做的是在#info的内容更改后分配点击处理程序。 Unfortunately .ready() is only an event handler for the document ready event. 不幸的是.ready()只是文档就绪事件的事件处理程序。 It only fires once. 它只会发射一次。 Also changing the html of '#info' isn't triggering any events (IMHO). 同样,更改'#info'的html不会触发任何事件(IMHO)。

You can work around this, using the .on -method on a parent element. 您可以通过在父元素上使用.on -method来解决此问题。 Consider this html structure: 考虑以下html结构:

<div id="info">
  <!-- This content is dynamically loaded -->
  <a id="nowydwor">Click this to change map</a>
  <!-- End of dynamic content -->
</div>

This makes it possible to call: 这样就可以调用:

$('#info').on('click', '#nowydwor', function(){ /* Change map here... */ })

This assigns the event handler to #info , which is only called if the clicked element matches '#nowydwor' . 这会将事件处理程序分配给#info ,仅当clicked元素匹配'#nowydwor'时才调用该事件处理程序。 Since '#info' is never removed, only the content changes, you don't have to apply it again. 由于永远不会删除'#info' ,因此仅更改内容,因此您无需再次应用它。

The only point is, you have to determine what the id of the map/place is, because the event handler will be the same for all links. 唯一的要点是,您必须确定地图/地点的ID是什么,因为所有链接的事件处理程序都相同。

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

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