简体   繁体   中英

How can I click an item by condition with Greasemonkey?

<input type="hidden" name="my_id" id="my_id" value="1">

<ul class="myList clearfix" id="thismyList">
<li class="myBullet" id="answer1">blabla1</li>
<li class="myBullet" id="answer2">blabla2</li>
<li class="myBullet" id="answer3">blabla3</li>

In this page I want to something like this:

if document.getElementById('my_id').value=="1"
document.getElementById('answer1').click;
if document.getElementById('my_id').value=="2"
document.getElementById('answer3').click;
if document.getElementById('my_id').value=="3"
document.getElementById('answer1').click;
if document.getElementById('my_id').value=="4"
document.getElementById('answer2').click;

I write this to show what I want. I know that code is wrong. But I think that explains what I want. How can I do that?

The correct syntax for that would be:

switch (document.getElementById('my_id').value) {
    case "1":
        document.getElementById ('answer1').click ();
        break;
    case "2":
        document.getElementById ('answer3').click ();
        break;
    case "3":
        document.getElementById ('answer1').click ();
        break;
    case "4":
        document.getElementById ('answer2').click ();
        break;
    default:
        break;
}

Note that in some cases, you may need to trigger mouse events like in this answer .

IF you want to invoke the click behaviour of a function you should just call.

document.getElementById('id').click();

I think it should works like you did without the '()' also.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM