【问题标题】:JQuery - Click button If button is present?JQuery - 单击按钮如果按钮存在?
【发布时间】:2016-11-25 03:30:52
【问题描述】:

我正在努力做到这一点:

有没有办法或 if 语句来调用它

$('#add-remove-buttons').find('.button').trigger('click');

在检测到网页上存在#add-remove-buttons 之后?

-谢谢

【问题讨论】:

  • “检测”是什么意思?按钮会动态添加到页面吗?

标签: jquery if-statement button input


【解决方案1】:

首先尝试检查第一个元素是否存在Check if element exists in jQuery 存在后触发事件后

 if ($("#add-remove-buttons").length > 0)
 { 
    $("#add-remove-buttons").trigger('click');
 }

【讨论】:

    【解决方案2】:

    当您在按钮上的单击处理程序之后插入$('#add-remove-buttons').trigger('click'); 语句时,如果该按钮存在,它将触发该操作。您无需明确检查它是否存在

    $(function(){
      
      $('#add-remove-buttons').on('click', function(){
        alert('btn clicked');
      })
      $('#add-remove-buttons').trigger('click');
    })
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <button id="add-remove-buttons">Click</button>

    【讨论】:

      猜你喜欢
      • 2018-11-05
      • 2012-08-02
      • 1970-01-01
      • 2023-03-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多