简体   繁体   English

如何在 javascript 文件中的 html 标签之间包含 javascript

[英]how to include javascript between html tags all within a javascript file

So I'm using React to create a web app and I'm just working with only js files and css files, no html. I have a js file like this with some html code and some js code:所以我正在使用 React 创建一个 web 应用程序,我只使用 js 文件和 css 文件,没有 html。我有一个像这样的 js 文件,其中包含一些 html 代码和一些 js 代码:

class Teams extends Component {
    state = {
        teams: []
    }

    componentDidMount() {
        ...
    }

    abc = () => {
        ...
    };

    render() {
        return (
            <Container>
               <Row className="space"></Row>
               <br></br>
               <br></br>
               <Row className="about-row"><h3>Teams</h3></Row>
               <br></br>
               <br></br>
               <Row className="about text-center">
                 {
                    this.abc()
                  }
               </Row>
            </Container>
        );
    }
}

export default Teams;

Basically I was wondering if there was a way to insert js code between html tags;基本上我想知道是否有办法在 html 标签之间插入 js 代码; Ie Is there a way to do something like this:即有没有办法做这样的事情:

<div>
   //js code 
   let x = "hello";
   console.log(x);
</div>

I've tried using html script tags and inserting js code in between but it doesn't work.我试过使用 html 脚本标签并在两者之间插入 js 代码,但它不起作用。 Would I have to make a function outside the html tags and just call that function from inside the tags?我是否必须在 html 标签之外制作一个 function 并从标签内部调用 function?

You can't declare variables inside of JSX code, but you can access the variables and functions declared before.你不能在 JSX 代码中声明变量,但你可以访问之前声明的变量和函数。 To insert JS inside JSX you have to use brackets {} around your JS code.要在 JSX 中插入 JS,您必须在 JS 代码周围使用方括号 {}。

ex:前任:

render() {
  let someVariable = {id: 1: name: "x"}
  console.log(someVariable)
  return(
    <div>
      {JSON.stringfy(someVariable)} // brackets here
    </div>
  )
}

Link to docs: How to add JS in JSX文档链接: 如何在 JSX 中添加 JS

So I'm using React to create a web app and I'm just working with only js files and css files, no html.所以我使用 React 创建一个 Web 应用程序,我只使用 js 文件和 css 文件,没有 html。 I have a js file like this with some html code and some js code:我有一个像这样的 js 文件,其中包含一些 html 代码和一些 js 代码:

class Teams extends Component {
    state = {
        teams: []
    }

    componentDidMount() {
        ...
    }

    abc = () => {
        ...
    };

    render() {
        return (
            <Container>
               <Row className="space"></Row>
               <br></br>
               <br></br>
               <Row className="about-row"><h3>Teams</h3></Row>
               <br></br>
               <br></br>
               <Row className="about text-center">
                 {
                    this.abc()
                  }
               </Row>
            </Container>
        );
    }
}

export default Teams;

Basically I was wondering if there was a way to insert js code between html tags;基本上我想知道是否有办法在 html 标签之间插入 js 代码; Ie Is there a way to do something like this:即有没有办法做这样的事情:

<div>
   //js code 
   let x = "hello";
   console.log(x);
</div>

I've tried using html script tags and inserting js code in between but it doesn't work.我试过使用 html 脚本标签并在中间插入 js 代码,但它不起作用。 Would I have to make a function outside the html tags and just call that function from inside the tags?我是否必须在 html 标签外创建一个函数,然后从标签内调用该函数?

Use brackets around your code so that React distinguishes it from HTML and doesn't render the code.在您的代码周围使用括号,以便 React 将其与 HTML 区分开来并且不呈现代码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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