繁体   English   中英

为什么我的 JavaScript 替换功能无法正常工作?

[英]Why can't I get my JavaScript replace function to work?

我已经尝试了很长一段时间来替换我的文本,但无论我尝试什么,它都没有替换任何内容。 我试过到处搜索,但找不到答案。

<html>
<head>
  <meta charset="UTF-8">
  <title>OnStream</title>
  <meta name="viewport" content="initial-scale=1, maximum-scale=1, user-scalable=0">



      <link rel="stylesheet" href="https://onstreamvideo.github.io/css/style.css">

</head>

<script language="JavaScript">
    function showInput() {
        document.getElementById('display').innerHTML =
                    document.getElementById("sharingurl").value;

    }
  </script>

  <script>
  function fetchCorrections() {
    var str = document.getElementById("sharingurl").innerHTML;
    var res = str.replace(/www.google.com/g, "google");
    document.getElementById("sharingurl").innerHTML = str;


}
    </script>



  <body onload="fetchCorrections();">
<header>
  <h1>OnStream</h1>
</header>
</body>

  <ul>
    <li>
      <form>
        Dropbox Share Url:<br>
  <input type="text" id="sharingurl" value="url"><br><br>

<input type="button" onclick="showInput();" value="Click"><br/>
  <br>
</form>
</li>
</ul>

<ul>
<li>
  <label>Your Input</label>
  <p><span id='display'</span></p>
</li>
<ul>

</body>
</html>

我是 Stack Overflow 和一般编码的新手,所以如果我犯了错误,请原谅我的无知。 :)

编辑:对不起,伙计们,我累了,我一整天都在随机工作,试图从每次停下来的地方开始,在发布之前我在代码中留下了一些错误。 我尝试稍微清理我的代码,并将上面的代码替换为更新的更干净的代码; 如果您发现任何错误,请随时告诉我,并理解这是我的一个笨拙的错误。 谢谢。

试试这个,它会起作用

<html>

<head>
  <meta charset="UTF-8">
  <title>OnStream</title>
  <meta name="viewport" content="initial-scale=1, maximum-scale=1, user-scalable=0">
  <link rel="stylesheet" href="https://onstreamvideo.github.io/css/style.css">
</head>
<script language="JavaScript">
  function showInput() {
    document.getElementById('display').innerHTML =
      document.getElementById("sharingurl").value;
  }

  function fetchCorrections() {
    showInput();
    var str = document.getElementById("sharingurl").value;
    var res = str.replace(/blue/g, "red");
    document.getElementById("sharingurl").value = res;
  }
</script>

<body class="depiction">
  <header>
    <h1>OnStream</h1>
  </header>
</body>

<body>
  <ul>
    <li>
      <form>
        Dropbox Share Url:
        <br>
        <input type="text" id="sharingurl" value="url">
        <br>
        <br>

        <input type="button" onclick="fetchCorrections()" value="Click">
        <br/>
        <br>
      </form>
    </li>
  </ul>

  <ul>
    <li>
      <label>Your Input</label>
      <p><span id='display' </p>
</li>
<ul>

</body>
</html>

程序中的错误

  1. id= 显示的跨度不完整。
  2. 输入元素没有像innerHTML这样的属性。
  3. 您应该使用 value 属性而不是 innerHTML 在 input 元素中分配值。
  4. 多个身体标签。

并尝试用 fetchcorrection 替换此功能

function fetchCorrection(){
    var str = document.getElementById("sharingurl").value;
    var res = str.replace(/blue/g, "red");
    document.getElementById("sharingurl").value = res;
}

暂无
暂无

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

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