用元素类模拟用javascript点击不工作

时间:2016-03-09 21:04:21

标签: javascript

我遇到了按元素类模拟点击的问题。我找到了按类名点击绿色按钮的代码。当你点击它时,它会隐藏2秒然后变得可见,但它没有t click again and i对它感到厌倦。 你可以看到我说的话:

enter image description here

var elem = document.getElementsByClassName("btn")[0];
    elem.addEventListener('click', function() {
        console.log('clicked')
    }, false);

    var support = true;

    try {
        if (new MouseEvent('click', {bubbles: false}).bubbles !== false) {
            support = false;
        } else if (new MouseEvent('click', {bubbles: true}).bubbles !== true) {
            support = false;
        }
    } catch (e) {
        support = false;
    }

    var refreshIntervalId = setInterval(function() {
        if (support) {
            var event = new MouseEvent('click');
        }else{
            var event = document.createEvent('Event');
            event.initEvent('click', true, true);
        }
        elem.dispatchEvent(event);
    },1);

我的事情,我需要一些东西来检查元素然后再次启动功能...... 如何使我的代码在可见时单击我的按钮? 任何想法?

1 个答案:

答案 0 :(得分:0)

试试这个:

    $('.element_class').click(function(e){
            //Do something

      //If you want to trigger a click on an element 
      $('.element2_class').trigger( "click" );

});