Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

105
Visualizações
Handle JS classes with import statements in html file?

I have 3 files.

  1. Connections.js
  2. Main.html.
  3. ScriptFile.js

I am copying only small part of the code here.

Connection.js has a class

import {net} from 'net';
export class connect {
Testfunction() {return "something"}
}

main.html has a function

<html> 
<head>
        <script src= 'Connections.js'></script>
        <script src= 'ScriptFile.js'></script> 
</head>
<body>
<p id="demo" onclick="myFunction()">Click me to change my text color.</p> 
</body>
</html>

ScriptFile.js

import { connect } from "./Connections.js";
const ObjConnect = new connect()
function myFunction() {
    document.getElementById("demo").innerHTML = ObjConnect.Testfunction();

  }

How do I make "text" in my html file to change to "something" when I click on it?

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

First you have a typo at file name. It's Connection, not Connections in main.js

Case1. using module

  • main.html
<html>
  <head>
    <script type="module" src="Connection.js"></script>
    <script type="module" src="ScriptFile.js"></script>
  </head>
  <body>
    <p id="demo">Click me to change my text color.</p>
  </body>
</html>
  • ScriptFile.js
import { connect } from './Connection.js';

const ObjConnect = new connect();
function myFunction() {
  document.getElementById('demo').innerHTML = ObjConnect.Testfunction();
}

document.getElementById('demo').addEventListener('click', myFunction);

Case2. using script

  • main.html
<html>
  <head>
    <script src="Connection.js"></script>
    <script src="ScriptFile.js"></script>
  </head>
  <body>
    <p id="demo" onclick="myFunction()">Click me to change my text color.</p>
  </body>
</html>
  • Connection.js
class connect {
  Testfunction() {
    return 'something';
  }
}
  • ScripFile.js
const ObjConnect = new connect();
function myFunction() {
  document.getElementById('demo').innerHTML = ObjConnect.Testfunction();
}
about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda