简体   繁体   English

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

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

I've been trying for quite a while to get my text to replace and it just hasn't been replacing anything no matter what I try.我已经尝试了很长一段时间来替换我的文本,但无论我尝试什么,它都没有替换任何内容。 I've tried searching everywhere but I can't find an answer.我试过到处搜索,但找不到答案。

<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>

I'm new to Stack Overflow and coding in general so please excuse my ignorance if I made a mistake.我是 Stack Overflow 和一般编码的新手,所以如果我犯了错误,请原谅我的无知。 :) :)

EDIT: Sorry guys, I'm tired and I've been working on this randomly throughout the day trying to pick up where I left off each time and I left a few errors in my code before posting.编辑:对不起,伙计们,我累了,我一整天都在随机工作,试图从每次停下来的地方开始,在发布之前我在代码中留下了一些错误。 I tried cleaning up my code a little bit, and I've swapped the code above for the newer more clean code;我尝试稍微清理我的代码,并将上面的代码替换为更新的更干净的代码; if you see any mistakes feel free to let me know and just understand it was a clumsy mistake on my part.如果您发现任何错误,请随时告诉我,并理解这是我的一个笨拙的错误。 Thanks.谢谢。

Try this out, it will 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;
  }

  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>

Mistakes in your programs程序中的错误

  1. Span with id= display is incomplete. id= 显示的跨度不完整。
  2. Input element doesn't have property like innerHTML.输入元素没有像innerHTML这样的属性。
  3. You should use value property instead of innerHTML to assign value in input element.您应该使用 value 属性而不是 innerHTML 在 input 元素中分配值。
  4. multiple body tags.多个身体标签。

And try to replace this function with fetchcorrection并尝试用 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