HTML Onclick with $(this)

孤人 提交于 2019-12-20 06:19:04

问题


I have a conflict with calling the general jQuery click function and so I need to call an onclick event on the HTML element itself while still being able to use $(this)

$('.item-main-section, .item-status').click( function () {
        var slideHeight = $(this).parent().children('.item-slide-section').height();
        if ($(this).parent().height() > 50) {
            $(this).parent().animate({height: '50px'}, 300);
            $(this).parent().children('item-slide-section').animate({bottom: '0px'}, 300);
        } else {
            $(this).parent().animate({height: slideHeight + 50 + 'px'}, 300);
            $(this).parent().children('item-slide-section').animate({bottom: slideHeight - 5 + 'px'}, 300);
            $(this).parent().siblings('.item-wrapper').animate({height: '50px'}, 300);
            $(this).parent().siblings('.item-wrapper').children('item-slide-section').animate({bottom: '0px'}, 300);
        }
    });

Instead of the jquery click i need onclick="thisFunction()" on the elements but if I get rid of the first line I lose the ability to use $(this). It's hard to explain but I hope it makes sense to someone.


回答1:


you can pass this element as parameter:

onclick="thisFunction($(this));"

and on function recieve it as element:

function thisFunction(element)
{
  $this = element;
  //use it here
}


来源:https://stackoverflow.com/questions/20367406/html-onclick-with-this

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!