当前位置: 动力学知识库 > 问答 > 编程问答 >

javascript - Chrome extension: ¿How to get href of button from class?

问题描述:

I want a javascript to check if there is a button in the webpage that is being visited with the class detail-w-button act_watchlink like the following:

<a href="link" class="detail-w-button act_watchlink">

And if that button exists, I want to store in a variable the href.

How can I do this automatically when the page loads?

Update:

I don't know if it helps, but I know that the page has the following code to listen to the button:

$('.act_watchlink').on('click', function(){...});

I think it would be fine to just triggering that action automatically.

网友答案:

How to check if a node exists with jQuery:

if($('a.detail-w-button.act_watchlink').length > 0)
    alert("I found it!");
else
    alert("There is no such button");

Get href of this button:

 var href = $('a.detail-w-button.act_watchlink').attr('href');

If you want to improve performance, store the button in a local variable instead of searching it each time you need it.

Update: If it is possible to encounter more than one such button on a page, you should address a specific item in the array of found objects. Like this:

var href = $('a.detail-w-button.act_watchlink').first().attr('href');
// note the first()

var href = $('a.detail-w-button.act_watchlink').eq(2).attr('href');
// note the eq(2)
网友答案:
$('.act_watchlink').trigger('click');
分享给朋友:
您可能感兴趣的文章:
随机阅读: