getElementByClassName onclick问题

时间:2018-02-15 17:15:17

标签: javascript jquery dom onclick

我想知道如何使用#include <ESP8266WiFi.h> #include <WiFiClient.h> const char *ssid = "ESPap"; const char *password = "thereisnospoon"; WiFiServer server(8080); void setup() { delay(1000); Serial.begin(115200); Serial.println(); Serial.print("Configuring access point..."); WiFi.softAP(ssid, password); IPAddress myIP = WiFi.softAPIP(); Serial.print("AP IP address: "); Serial.println(myIP); server.begin(); } void loop() { WiFiClient clie = server.available(); if(clie) { while(clie.connected()) { if(clie.available()) { Serial.println(clie.read()); } } clie.stop(); } } 检测到该按钮被点击了。该按钮没有ID,是作为嵌入式iframe的一部分生成的。

getElementByClassName

我正在尝试这个脚本<button class="playButton medium" role="application" title="Play" style="color: rgb(204)</button> 如果单击按钮,但它似乎对我不起作用。

alert

感谢您的任何建议

1 个答案:

答案 0 :(得分:3)

首先是getElementsByClassName(),请注意复数s。其次,您只能提供一个类作为该函数的参数。此外,最好使用addEventListener()优于on*事件属性。试试这个:

var x = document.getElementsByClassName("playButton");
for (var i = 0; i < x.length; i++) {
  x[i].addEventListener('click', function() {
    console.log("clicked");
  });
};
<button class="playButton medium" role="application" title="Play" style="color: rgb(204)">Play</button>

如果您想使用这两个类选择元素,则可以使用document.querySelectorAll('.playButton.medium')

相关问题