【问题标题】:How to Transfer Working HTML, CSS, Javascript Code from Codepen to Visual Studio Code and Browser如何将工作 HTML、CSS、Javascript 代码从 Codepen 传输到 Visual Studio 代码和浏览器
【发布时间】:2019-09-21 13:24:20
【问题描述】:

我的代码在 codepen 上工作,但不能在外面使用 Visual Studio Code 创建我的文件(它们是:Quote.htmlQuote.cssQuote.js 都在同一个文件夹中)。

当我在浏览器中打开我的html 文件时,我得到一个绿屏,因此css 文件链接正确,但js 文件链接不正确。

根据我在 Stackoverflow 上阅读的内容,我正确地将 js 文件放入脚本中,但我所做的事情是错误的。我

HTML 文件代码:

    <!DOCTYPE html>
    <html lang="en">
    <head>
        <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous"/>
        <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/font-awesome/4.5.0/css/font-awesome.min.css" integrity="sha384-XdYbMnZ/QjLh6iI4ogqCTaIjrFk87ip+ekIjefZch0Y+PvJ8CDYtEs1ipDmPorQ+" crossorigin="anonymous">
        <meta name="viewport" content="width=device-width, initial-scale=1">
        <meta charset="utf-8">
        <link rel="stylesheet" type="text/css" href="Quote.css">
    </head>
    <body>
        <div id="app"></div>
        <script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.8.6/umd/react.production.min.js" crossorigin></script>
        <script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.8.6/umd/react-dom.production.min.js" crossorigin></script>
        <script src="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.1.3/css/bootstrap.min.css"></script>
        <script src="Quote.js" type="text/javascript"></script>
    </body>
    </html>

//css file code:

    body {background-color: green; color: white;}
    #quote-box {
    margin-top: 80px;
}

Javascript 文件代码:

    const quotes = [
    {
    quote: "Don't cry because it's over, smile because it happened.",
    author: "Dr. Seuss"
    },
    {
    quote: "You only live once, but if you do it right, once is enough.",
    author: "Mae West"
    },
    {
    quote: "Be yourself; everyone else is already taken.",
    author: "Oscar Wilde"
    },
    {
    quote:
    "Two things are infinite: the universe and human stupidity; and I'm not sure about the universe.",
    author: "Albert Einstein"
    },
    { quote: "So many books, so little time.", author: "Frank Zappa" },
    {
    quote: "A room without books is like a body without a soul.",
    author: "Marcus Tullius Cicero"
    },
    {
    quote:
    "If you want to know what a man's like, take a good look at how he treats his inferiors, not his equals.",
    author: "J.K. Rowling"
    }
    ];
    class Presentational extends React.Component {
    constructor(props) {
      super(props);
      this.state = {
        quote: "If you want to know what a man's like, take a good look at how he treats his inferiors, not his equals.", author: "J.K. Rowling"
      }
    this.newQuote = this.newQuote.bind(this);
    this.sendTweet = this.sendTweet.bind(this);
    }
    newQuote() {
    const randNumber = Math.floor(Math.random() * quotes.length);
    this.setState({quote: quotes[randNumber].quote, author: quotes[randNumber].author})
    }
    sendTweet = () => {
    const url = "twitter.com";
    const text = this.state.quote.concat(" - ").concat(this.state.author); 
    window.open('http://twitter.com/share?url='+encodeURIComponent(url)+'&text='+encodeURIComponent(text), '', 'left=0,top=0,width=550,height=450,personalbar=0,toolbar=0,scrollbars=0,resizable=0');
    }
    render() {
      return (
        <div id="quote-box" class="container">
        <div class="row">
          <h1 class="col-md-3"></h1>
          <h1 class="text-center col-md-6">Random Quotes:</h1>
          <h1 class="col-md-3"></h1>
        </div>  
        <div class="row">
          <p class="col-md-3"></p>
          <blockquote class="col-md-6">
           <p id="text"><i class="fa fa-quote-left"></i> {this.state.quote} <i class="fa fa-quote-right"></i></p>
               <cite id="author">-- {this.state.author}</cite>
          </blockquote>
          <p class="col-md-3"></p>
        </div>  
        <div class="row">  
          <p class="col-md-3"></p>
          <button id="new-quote" class="btn btn-default col-md-1" onClick={this.newQuote}>New Quote</button>
          <p class="col-md-3"></p>
            <a  id="tweet-quote" onClick={this.sendTweet} class="text-right"><button class="btn btn-default col-md-2">Tweet Quote <i class="fa fa-twitter"></i></button></a>
          <p class="col-md-3"></p>
        </div>  
        </div>
      );
    }
    };
    ReactDOM.render(<Presentational />, document.getElementById("app"));

这是指向应显示内容的 Codepen 链接:https://codepen.io/EOJA/pen/MRNoBq

【问题讨论】:

  • 您是否设置了本地环境...?
  • 我正在学习编码,从我通过谷歌搜索“本地环境”阅读的内容来看,我的理解是它已设置 - 这意味着我有一些 .html、.css 和 .js 文件我电脑上的文件夹。使用来自互联网的示例,以相同的方式保存到我的计算机,我可以使用浏览器打开 .html 文件来查看网站。但是,对于我的问题中的文件,我无法查看该网站 - 它只是一个绿屏。
  • 我对本地环境的理解有误吗?

标签: javascript html css reactjs twitter-bootstrap


【解决方案1】:

看看你的代码,你的项目目录应该是这样的:

Project_Folder
| index.html
| Quote.css
| Quote.js

编辑:在调查了一段时间后,我找到了this来帮忙。

<!DOCTYPE html>

<html lang="en">

<head>
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous" />
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/font-awesome/4.5.0/css/font-awesome.min.css" integrity="sha384-XdYbMnZ/QjLh6iI4ogqCTaIjrFk87ip+ekIjefZch0Y+PvJ8CDYtEs1ipDmPorQ+" crossorigin="anonymous">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <meta charset="utf-8">
    <style>
        body {
            background-color: green;
            color: white;
        }
        
        #quote-box {
            margin-top: 80px;
        }
    </style>

    <script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react.min.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react-dom.min.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/babel-standalone/6.24.0/babel.js"></script>
</head>

<body>
    <div id="app"></div>
    <script src="https://cdn.freecodecamp.org/testable-projects-fcc/v1/bundle.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.8.6/umd/react.production.min.js" crossorigin></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.8.6/umd/react-dom.production.min.js" crossorigin></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.1.3/css/bootstrap.min.css"></script>
    <script type='text/babel'>
       // !! IMPORTANT README:

// You may add additional external JS and CSS as needed to complete the project, however the current external resource MUST remain in place for the tests to work. BABEL must also be left in place. 
const quotes = [
  {
    quote: "Don't cry because it's over, smile because it happened.",
    author: "Dr. Seuss"
  },
  {
    quote: "You only live once, but if you do it right, once is enough.",
    author: "Mae West"
  },
  {
    quote: "Be yourself; everyone else is already taken.",
    author: "Oscar Wilde"
  },
  {
    quote:
    "Two things are infinite: the universe and human stupidity; and I'm not sure about the universe.",
    author: "Albert Einstein"
  },
  { quote: "So many books, so little time.", author: "Frank Zappa" },
  {
    quote: "A room without books is like a body without a soul.",
    author: "Marcus Tullius Cicero"
  },
  {
    quote:
    "If you want to know what a man's like, take a good look at how he treats his inferiors, not his equals.",
    author: "J.K. Rowling"
  }
];
class Presentational extends React.Component {
	constructor(props) {
	  super(props);
	  this.state = {
		quote: "If you want to know what a man's like, take a good look at how he treats his inferiors, not his equals.", author: "J.K. Rowling"
	  }
    this.newQuote = this.newQuote.bind(this);
    this.sendTweet = this.sendTweet.bind(this);
	}
  newQuote() {
    const randNumber = Math.floor(Math.random() * quotes.length);
    this.setState({quote: quotes[randNumber].quote, author: quotes[randNumber].author})
  }
  sendTweet = () => {
    const url = "twitter.com";
    const text = this.state.quote.concat(" - ").concat(this.state.author); 
    window.open('http://twitter.com/share?url='+encodeURIComponent(url)+'&text='+encodeURIComponent(text), '', 'left=0,top=0,width=550,height=450,personalbar=0,toolbar=0,scrollbars=0,resizable=0');
  }
	render() {
	  return (
		<div id="quote-box" class="container">
        <div class="row">
          <h1 class="col-md-3"></h1>
          <h1 class="text-center col-md-6">Random Quotes:</h1>
          <h1 class="col-md-3"></h1>
        </div>  
        <div class="row">
          <p class="col-md-3"></p>
          <blockquote class="col-md-6">
           <p id="text"><i class="fa fa-quote-left"></i> {this.state.quote} <i class="fa fa-quote-right"></i></p>
  			   <cite id="author">-- {this.state.author}</cite>
          </blockquote>
          <p class="col-md-3"></p>
        </div>  
        <div class="row">  
          <p class="col-md-3"></p>
          <button id="new-quote" class="btn btn-default col-md-1" onClick={this.newQuote}>New Quote</button>
          <p class="col-md-3"></p>
  			<a  id="tweet-quote" href="http://twitter.com/intent/tweet" onClick={this.sendTweet} class="text-right"><button class="btn btn-default col-md-2">Tweet Quote <i class="fa fa-twitter"></i></button></a>
          <p class="col-md-3"></p>
        </div>  
		</div>
	  );
	}
  };
ReactDOM.render(<Presentational />, document.getElementById("app"));


    </script>
</body>

</html>

【讨论】:

  • 我将 html 文件的名称从 Quote.html 更改为 index.html 但我仍然得到相同的结果 - 绿屏(背景颜色由 css 文件设置为绿色)但没有js文件渲染。
  • html文件的名称对这个问题没有影响。我只是注意到在 codepen 中你使用的是 babel。最简单的方法就是使用查看编译的 JS(您可以在 js 选项卡下拉选项中找到)。如果没有,你真的需要设置你的本地环境。
  • @eoaja 如果可行,请参考更新后的解决方案。
  • @eoaja,很高兴为您提供帮助。你能选择我的答案作为解决方案吗?谢谢。
【解决方案2】:

不是 React 专家,但我相信您缺少 Babel。

在这种情况下,我可以通过内联您的 Javascript 并将 Babel 作为另一个脚本源来获得示例。

<!-- Bottom of index.html -->   
<script src="https://cdnjs.cloudflare.com/ajax/libs/babel-standalone/6.21.1/babel.min.js"></script>
<script type="text/babel">
... Your JS code
<script>

参考:Why does React require Babel and Webpack to work?


编辑:

您可以通过引用 Quote.js 来避免内联您的 Javascript:

&lt;script type='text/babel' src='Quote.js'&gt;&lt;/script&gt;

然后启动本地服务器并以这种方式调用文件。

参考:How do you set up a local testing server?

【讨论】:

  • 是的!问题是通天塔!我不得不将编译好的 js 放到 Quote.js 文件中,它才能在不使用 inline 方法的情况下工作。
猜你喜欢
  • 2021-05-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-03-30
  • 1970-01-01
  • 2022-08-11
  • 2015-05-17
  • 1970-01-01
相关资源
最近更新 更多