使用svg类单击所有按钮

Click all buttons with svg class

本文关键字:按钮 单击 svg 使用      更新时间:2024-01-26

我想点击这个类下页面上的所有按钮

<svg class="opinionate-action post-agree" version="1.1" viewBox="0 0 14.7 18.4" data-reactid=".0.2.1.0.2.0.$57047af48bd3ef3c0ddf7dd8.$57047af48bd3ef3c0ddf7dd8.0.1.5.1.0.0.0" style="-webkit-user-select: auto;"><g data-reactid=".0.2.1.0.2.0.$57047af48bd3ef3c0ddf7dd8.$57047af48bd3ef3c0ddf7dd8.0.1.5.1.0.0.0.0" style="-webkit-user-select: auto;"><path d="M14.4,12.3c0.2-0.4,0.3-0.9,0.3-1.4c0-0.7-0.3-1.4-0.7-2c0-0.1,0-0.2,0-0.4c0-1.7-1.3-3.2-3-3.3 c0-0.3,0.1-0.7,0.1-1.1c0-2.3-1.7-4-4.1-4c-0.3,0-0.4,0-0.4,0L4.8,0.4v2.9C4.4,4.2,3.5,4.9,3,5.1l0,0C1.3,5.9, 0.1,7.8,0.1,9.9v3.3 v0.1c0,2.8,2.3,5.2,5.2,5.2h5.3c1.8,0,3.2-1.3,3.2-3c0-0.1,0-0.2,0-0.2c0.5-0.5,0.8-1.2, 0.8-2C14.6,12.9,14.5,12.6,14.4,12.3z M12.6,13.2c0,0.6-0.6,1-1.2,1h-0.5c0.4,0.1,1,0.7,1,1.2c0,0.6-0.6,1-1.2, 1H5.3c-1.7,0-3.2-1.4-3.2-3.2V9.9c0-1.2,0.7-2.5,1.8-3 c0.8-0.6,2.5-1.5,2.9-3.4V2.2c0,0,2.4-0.3,2.4,2c0, 2.6-1.6,3-0.3,3.1h2c0.7,0,1.3,0.7,1.3,1.4c0,0.6-0.6,1-1,1.1h0.5 c0.6,0,1.2,0.6,1.2,1.3c0,0.6-0.6,1-1.2, 1h0C12,12.1,12.6,12.6,12.6,13.2z" data-reactid=".0.2.1.0.2.0.$57047af48bd3ef3c0ddf7dd8.$57047af48bd3ef3c0ddf7dd8.0.1.5.1.0.0.0.0.0" style="-webkit-user-select: auto;"></path></g></svg>

用jQuery:试试这个

$(function(){
    $('.opinionate-action.post-agree').on('click',function(e){
    console.log('click !');
    // the code you want to execute here
    });
})

你好!弗雷德里克

我认为您希望实际点击所有按钮,而不是设置点击处理程序。不确定现实世界中的用例是什么,但请使用jQuery尝试一下。

$(function(){
    $('.opinionate-action.post-agree').click();
})

使用$('document').ready加载整个页面和点击按钮后,因为没有它,你可以点击尚未加载的元素(这意味着你什么都没点击)

  $('document').ready(function f(){
  $('.opinionate-action.post-agree').click();
  });

或者如果需要排除某些元素

  $('.opinionate-action.post-agree').each(function () {if($(this).attr('some_attr')!='1') $(this).click();} );

或者为什么不只使用JS

 var a= document.getElementsByClassName('opinionate-action');
 var a= document.querySelectorAll('[class^=opinionate-action]');
 var a= getElementsByAttribute('data-reactid');
 for(var i = 0; i < a.length; i++)
 { 
 a[i].click();
 console.log(a[i]);
 }