如果没有ID,我如何点击按钮

时间:2022-07-01 00:57:20

I would like to click on the button that is named Envoi, but I can't find an id to reference it by using getElementById("Envoi").click. Is there any other way to do this?

我想点击名为Envoi的按钮,但我找不到使用getElementById(“Envoi”)引用它的id。点击。有没有其他方法可以做到这一点?

The website name is : http://famille-frappier.fr/ and the button name is Envoi.

网站名称为:http://famille-frappier.fr/,按钮名称为Envoi。

And the associated html is:

相关的html是:

<input type="button" value="Envoi">

7 个解决方案

#1


3  

If you can use jQuery you could do

如果你可以使用jQuery,你可以做到

$('input[value="Envoi"]').click();

#2


3  

With pure Javascript:

使用纯Javascript:

I saw your site, you don't use jQuery, so my solution also doesn't use it.

我看到了你的网站,你不使用jQuery,所以我的解决方案也没有使用它。

In your case, you have no id or name in your button, you have only the value, so you need to use this:

在您的情况下,您的按钮中没有ID或名称,您只有值,因此您需要使用此:

document.querySelector('input[value="Envoi"]').click()

#3


1  

To obtain the element, you could use document.getElementsByTagName() to get all the elements, then loop through them to find the one you're interested in:

要获取元素,可以使用document.getElementsByTagName()来获取所有元素,然后遍历它们以找到您感兴趣的元素:

var elements = document.getElementsByTagName("input");
var envoi;
for (var i in elements) {
    if (elements[i].getAttribute && elements[i].getAttribute("value") == "Envoi") {
        envoi = elements[i];
        break;
    }
}

#4


0  

var els = document.getElementsByTagName("input");
for(var i=0; i<els.length; i++){
  if(els[i].value=='Envoi') //do something with els[i], thats your button
}

#5


0  

Well, if you type this into the console, you get the word Envoi displayed.

好吧,如果你在控制台中键入它,你会看到Envoi这个词。

document.getElementById('nav_554').getElementsByTagName('input')[0].value

Therefore, you can use the expression to attach a click event listener. Or of course, you could just add the onclick attribute to the button itself.

因此,您可以使用表达式附加单击事件侦听器。或者当然,您只需将onclick属性添加到按钮本身即可。

This code typed into the console will attach a function to that button:

键入控制台的此代码会将一个函数附加到该按钮:

document.getElementById('nav_554').getElementsByTagName('input')[0].addEventListener('click', function(){alert("you clicked me");}, false);

No need for jQuery - total overkill for this simple task.

不需要jQuery - 对于这个简单的任务总是过度杀伤。

#6


-1  

You can use document.getElementsByName() to retrieve the button.

您可以使用document.getElementsByName()来检索按钮。

var button = document.getElementsByName('Envoi');
button.click ();

Here's an example

这是一个例子

#7


-1  

The easiest way in my opinion is using JQuery.

我认为最简单的方法是使用JQuery。

$('[name="Envoi"]').click();

Just download the JQuery.js and include it in the page and the above statement will work. For more information view http://api.jquery.com

只需下载JQuery.js并将其包含在页面中,上面的语句就可以了。有关更多信息,请访问http://api.jquery.com

#1


3  

If you can use jQuery you could do

如果你可以使用jQuery,你可以做到

$('input[value="Envoi"]').click();

#2


3  

With pure Javascript:

使用纯Javascript:

I saw your site, you don't use jQuery, so my solution also doesn't use it.

我看到了你的网站,你不使用jQuery,所以我的解决方案也没有使用它。

In your case, you have no id or name in your button, you have only the value, so you need to use this:

在您的情况下,您的按钮中没有ID或名称,您只有值,因此您需要使用此:

document.querySelector('input[value="Envoi"]').click()

#3


1  

To obtain the element, you could use document.getElementsByTagName() to get all the elements, then loop through them to find the one you're interested in:

要获取元素,可以使用document.getElementsByTagName()来获取所有元素,然后遍历它们以找到您感兴趣的元素:

var elements = document.getElementsByTagName("input");
var envoi;
for (var i in elements) {
    if (elements[i].getAttribute && elements[i].getAttribute("value") == "Envoi") {
        envoi = elements[i];
        break;
    }
}

#4


0  

var els = document.getElementsByTagName("input");
for(var i=0; i<els.length; i++){
  if(els[i].value=='Envoi') //do something with els[i], thats your button
}

#5


0  

Well, if you type this into the console, you get the word Envoi displayed.

好吧,如果你在控制台中键入它,你会看到Envoi这个词。

document.getElementById('nav_554').getElementsByTagName('input')[0].value

Therefore, you can use the expression to attach a click event listener. Or of course, you could just add the onclick attribute to the button itself.

因此,您可以使用表达式附加单击事件侦听器。或者当然,您只需将onclick属性添加到按钮本身即可。

This code typed into the console will attach a function to that button:

键入控制台的此代码会将一个函数附加到该按钮:

document.getElementById('nav_554').getElementsByTagName('input')[0].addEventListener('click', function(){alert("you clicked me");}, false);

No need for jQuery - total overkill for this simple task.

不需要jQuery - 对于这个简单的任务总是过度杀伤。

#6


-1  

You can use document.getElementsByName() to retrieve the button.

您可以使用document.getElementsByName()来检索按钮。

var button = document.getElementsByName('Envoi');
button.click ();

Here's an example

这是一个例子

#7


-1  

The easiest way in my opinion is using JQuery.

我认为最简单的方法是使用JQuery。

$('[name="Envoi"]').click();

Just download the JQuery.js and include it in the page and the above statement will work. For more information view http://api.jquery.com

只需下载JQuery.js并将其包含在页面中,上面的语句就可以了。有关更多信息,请访问http://api.jquery.com