开发者

Click event failing on div in jQuery

I am using the below code:

<div class="business-bottom-content" onMouseOver="javascript:this.className='business-bottom-    content-hover';"  onMouseOut="javascript:this.clas开发者_如何学运维sName='business-bottom-content';"> 

The jQuery script is as following:

 $("div.business-bottom-content").click(
 function()
  {
   alert('ashutosh mishra');
  });


You probably define the div after the script has executed.

Wrap it in $(document).ready(function() { .... }); to ensure it executes after the full DOM is available.

Additionally you should get rid of those ugly inline events (if you even need them - you can use :hover in CSS):

$('div.business-bottom-content').hover(function() {
    $(this).addClass('business-bottom-content-hover');
}, function() {
    $(this).removeClass('business-bottom-content-hover');
});


You can try this for div class have to mention "."

 $(".business-bottom-content").click(
 function()
 {
      alert('ashutosh mishra');
 });


In some scenarios even wrapping it in a $(document).ready(function() { .... }); may not be enough. In that case, just use a timeout function.

HTML

<div id="up-button" class="inc button-number-cart">
 <span class="up-arrow">+</span>
</div> 

JS

setTimeout(function(){
        $(document).on('click','.button-number-cart',function(){
            event.preventDefault();


            const button = $(this);
            const oldValue = button.parent().parent().find('input').val();
            let newVal;

            if (button.find('.up-arrow').length !== 0) {
                newVal = parseFloat(oldValue) + 1;
            } else {
                if (oldValue > 0) {
                    newVal = parseFloat(oldValue) - 1;
                } else {
                    newVal = 0;
                }
            }
            button.parent().parent().find('input').val(newVal);
        });
 },700);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜