No he aprendido ningún otro lenguaje de programa y comencé a aprender html hoy, estoy creando un programa que muestra un lugar aleatorio en el mapa de Google El enlace del mapa de Google es este y https://www.google.co.kr/maps/@36.3904052,127.331469 ,18.09z Quiero dar un enlace como ( https://www.google.co.kr/maps/@latitude,longitude,18.09z ) este
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>random palce</title> </head> <body style="background-color:#FAD0C9;"> <div style="text-align:center"> <strong> <p style="font-size: 70px; color:#6E6E6D">click the button</p> </strong> <script> var random1 = (Math.floor(Math.random() * (381601797 - 341601796)) + 341601796)/10000000; document.write( '<p>' + random1 + '</p>' ); var random2 = (Math.floor(Math.random() * (1289677998 - 1262365489)) + 1262365489)/10000000; document.write( '<p>' + random2 + '</p>' ); </script> <button onclick="location.href='https://www.google.co.kr/maps/' " style="width: 350px; height: 150px; font-size: 40px; background-color:#D64161; color:#364b44; border:none; ">random place</button> </div> </body>este es el código que hice. Quiero editar <button onclick="location.href='https://www.google.co.kr/maps/' esta parte para mostrar un enlace como ( https://www.google.co .kr/maps/@latitud,longitud,18.09z )
En primer lugar, el evento onclick es Javascript, no html.
Luego, podría usar literales de plantilla para insertar variables en una cadena o una URL. Además, no le sugiero que escriba un código Javascript largo en onclick . Use addEventListener en el script en su lugar.
Los literales de plantilla son literales delimitados con acentos graves (`), lo que permite expresiones incrustadas denominadas sustituciones.
* Realmente no sé cómo debería verse el formato de URL de solicitud para el mapa de Google, pero escribir directamente las coordenadas parece estar mal. Tienes que averiguarlo por ti mismo.
var random1 = (Math.floor(Math.random() * (381601797 - 341601796)) + 341601796) / 10000000; document.write('<p>' + random1 + '</p>'); var random2 = (Math.floor(Math.random() * (1289677998 - 1262365489)) + 1262365489) / 10000000; document.write('<p>' + random2 + '</p>'); document.querySelector('button').addEventListener('click', function() { location.href = `https://www.google.co.kr/maps/${random1+random2}` }) <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>random palce</title> </head> <body style="background-color:#FAD0C9;"> <div style="text-align:center"> <strong> <p style="font-size: 70px; color:#6E6E6D">click the button</p> </strong> <button style="width: 350px; height: 150px; font-size: 40px; background-color:#D64161; color:#364b44; border:none; ">random place</button> </div> </body>