简体   繁体   English

单击时弹出的窗口重复

[英]window pop up duplicated when click

I have this JS: 我有这个JS:

  $(document).ready(function() {
       $("ul.social").hide();
       $("span.link").each(function(i){
         $(this).mouseover(function(){
           $("ul.social").eq(i).show("fast", function(){
             $("ul.social li a").click(function(){
               var link = this.href;
               window.open(link, " ", "height=350,width=600");
             });
           });
         });
       });
     });

And this HTML : 和这个HTML:

<ul class="social">
    <li><a href="http://www.linkedin.com/shareArticle?mini=true&amp;url=<?php the_permalink() ?>" title="Compartilhar <?php the_title(); ?> no LinkedIn" target="_blank"><img src="<?php bloginfo('template_url'); ?>/img/in.png" alt="" width="24" /></a></li>
    <li><a href="https://plus.google.com/share?url=<?php the_permalink() ?>" title="Compartilhar <?php the_title(); ?> no Google Plus" target="_blank"><img src="<?php bloginfo('template_url'); ?>/img/gp.png" alt="" width="24" /></a></li>
    <li><a href="http://www.facebook.com/sharer.php?u=<?php the_permalink() ?>" title="Compartilhar <?php the_title(); ?> no Facebook" target="_blank"><img src="<?php bloginfo('template_url'); ?>/img/fb.png" alt="" width="24" /></a></li>
    <li><a href="http://twitter.com/share?url=<?php the_permalink() ?>&amp;text=<?php the_title(); ?>&amp;via=chocoladesign" title="Compartilhar <?php the_title(); ?> no Twitter" target="_blank"><img src="<?php bloginfo('template_url'); ?>/img/tw.png" alt="" width="24" /></a></li>
</ul>

But, whenever i click on (ul.social li a) , he's duplicate the window. 但是,每当我单击(ul.social li a) ,他都会复制该窗口。 one of these opens at size 600x350 and the other opens full screen. 其中一个打开尺寸为600x350 ,另一个打开全屏。

Why this happen? 为什么会这样?

It's because when you click on that link, it is doing the default action of opening the link, like it normally would, and then it's opening it again with your window.open code. 这是因为,当您单击该链接时,它会像往常一样执行打开链接的默认操作,然后使用window.open代码再次打开它。 So just add event.preventDefault() so that it stops the first opening from happening. 因此,只需添加event.preventDefault()即可阻止第一次打开。

 $("ul.social li a").click(function(event){
               event.preventDefault();
               var link = this.href;
               window.open(link, " ", "height=350,width=600");

Its because, the default action of <a> is not prevented, due to which even after opening the popup, the link opens in a new tab. 这是因为,没有阻止<a>的默认操作,因此即使打开了弹出窗口,该链接也会在新选项卡中打开。

Try this. 尝试这个。

 $("ul.social li a").click(function(e){
   e.preventDefault();
   var link = this.href;
   window.open(link, " ", "height=350,width=600");
 });

For further info, refer this 有关更多信息,请参阅

dont attach click event inside moseover event , u will get duplicated click event every time you mose over. 不要将点击事件附加在moseover事件内,每次您进行修改时,您都会获得重复的点击事件。

and $("ul.social li a").click is wrong , you are attaching a click to every link and not just $("ul.social").eq(i) $("ul.social li a").click错误,您将点击附加到每个链接上,而不仅仅是$("ul.social").eq(i)

$(document).ready(function() {
    $("ul.social li a").click(function(e){ 
        e.preventDefault();
        var link = this.href;
        window.open(link, " ", "height=350,width=600");
     });
       $("ul.social").hide();
       $("span.link").each(function(i){
         $(this).mouseover(function(){
           $("ul.social").eq(i).show("fast", function(){

           });
         });
       });
     });

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

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