简体   繁体   English

点击事件无法正常运行

[英]Click event not working properly

I have a problem with click event: 我的点击事件有问题:

I have in HTML: 我有HTML:

<div class="content-block cards-clipboard">
                        <a class="download-all">Download All</a>

                        </div>

And a JS: 还有一个JS:

var html = '<div class="card demo-card-header-pic data-url='+img+'">'+
  '<div style="background-image:url('+thumb+'); background-size: 100%; height: 272px; background-repeat: no-repeat;" valign="bottom" class="card-header color-white no-border">'+html_icone_camera+'</div>'+
  '<div class="card-content">'+
    '<div class="card-content-inner">'+
     '<p class="color-gray">Postado em '+datapub+'</p>'+
      '<p>'+legenda+'</p>'+
    '</div>'+
  '</div>'+
  '<div class="card-footer-'+id_link+'">'+
    '<button data-url='+img+' data-step="download" data-cod='+id_link+' type="button" class="download-imagem btn btn-primary btn-lg btn-block '+id_link+'"><i class="fa fa-download" aria-hidden="true"></i>&nbspDownload</button>'+
  '</div>'+
'</div>';

$$(html).appendTo('.cards-clipboard');  

And a trigger: 和一个触发器:

$$('.download-all').on('click', function() {
                $$("button[data-step='download']").click();

            });

That is: I have a button ('.download-all') when clicked, trigger a click in other buttons generated dinamically via append (var html) that have the data-step=download attribute. 那就是:单击时有一个按钮('.download-all'),触发其他通过append (var html)动态生成的具有data-step = download属性的按钮的单击。

However, when i click in .download-all button, the first button not receive a click - only the first button - the others, receive.... 但是,当我单击.download-all按钮时,第一个按钮未获得点击-只有第一个按钮-其他获得了...。

I appreciate any help 感谢您的帮助

EDIT : If i put $$("button[data-step='download']").click(); 编辑 :如果我把$$("button[data-step='download']").click(); in console, the clicks works fine in all buttons.... 在控制台中,单击在所有按钮中都正常。

If you are using jQuery, remove 2 $ signs and only have one: 如果您使用的是jQuery,请删除2个$符号,并且只有一个:

$(html).appendTo('.cards-clipboard');  

And your event command would be: 您的事件命令将是:

$('.download-all').on('click', function() {
            $("button[data-step='download']").trigger('click');

        });

Try to use JQuery trigger() 尝试使用JQuery trigger()

 $$('.download-all').on('click', function() {
            $$("button[data-step='download']").trigger("click");

 });

If not try to remove the double dollar sign and use single dollar sign. 如果不是,请尝试删除双美元符号并使用单美元符号。

Please vote and mark the solution if useful. 如果有用,请投票并标记解决方案。

Thanks! 谢谢!

Only the first button is being triggered because of page loading order. 由于页面加载顺序,仅第一个按钮被触发。 php is executed before html/javascript. php在html / javascript之前执行。 Because this happens, the event handler can't find a button with specified id or class. 因为发生这种情况,所以事件处理程序找不到具有指定ID或类的按钮。 One way to circumvent this is to delegate an event listener to the area which contains the dynamically created buttons. 避免这种情况的一种方法是将事件侦听器委托给包含动态创建的按钮的区域。 For instance, 例如,

$('#myId').delegate('button','click',function() {
       //do something
});

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

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