Safari JS innerHTML错误/错误

时间:2017-08-30 09:01:59

标签: javascript html safari innerhtml

我在一个非常简单的表单验证脚本中遇到了一个奇怪的问题。使用Chrome和Firefox没有任何问题,但使用safari我的<p>的innerHTML无法正确更改。第二个问题是我无法在代码段中显示它,因为代码片段太小。如果用户首先键入@然后输入一个数字并再次删除该数字,则会收到混合错误消息。 (错误消息3和消息6的第二行)。如果我只是手动重新缩放safari窗口,第二行就会消失。使用/和数字或a的组合也会发生同样的情况。和一个数字。就像你在代码中看到的一样。

网站预览:Preview

为了产生错误/错误,我在绿色框中的第三个img中解决了这个问题: 填写@ Step 1 添加一个号码 Step 2 删除号码 Step 3

这是我的HTML:

<h3 id="forms">Forms</h3>
<div class="forms">
    <form>
        <div class="input"><input placeholder="name" name="name" type="text" oninput="checkname(this.id)" id="name"><div><p id="namereaction" class="reaction"></p></div></div>
        <div class="input"><input placeholder="firstname" name="name" type="text" oninput="checkname(this.id)" id="firstname"><div><p id="firstnamereaction" class="reaction"></p></div></div>
        <div class="input"><input placeholder="mail" name="mail" type="email" oninput="checkmail()" id="mail"><div><p id="mailreaction" class="reaction"></p></div></div>
        <div class="input"><textarea name="text" id="textarea"></textarea><div id="textreactionbox"><p id="textareareaction" class="reaction"></p></div></div>
        <div class="input"><input type="submit" name="submit" value="submit"></div>
    </form>
</div>

我的JS:

function checkname(id){
    var error="";
    var value = document.getElementById(id).value;
    if(value.length<1){
        error = "Please fill in your (first)name.";
    }
    if(value.length>50){
        error = "Please fill in your (first)name. Your name is to long.";
    }
    if (value.indexOf("@") > -1 ) {
        error = "I think you are filling in your emailadress";
    }
    if (value.indexOf(".") > -1 ) {
        error = "Please check your (first)name, most names don't have a '.'.";
    }
    if (value.indexOf("/") > -1 ) {
        error = "Please check your (first)name, most names don't have a '/'.";
    }
    var matches = value.match(/\d+/g);
    if (matches != null) {
        error = "Please check your (first)name, we are all people, our (first)name doesn't contain a number.";
    }
    if(error.length>1){
        document.getElementById(id+"reaction").innerHTML=error;
    }else{
        document.getElementById(id+"reaction").innerHTML="";
    }
}

1 个答案:

答案 0 :(得分:1)

这似乎是Safari的渲染错误,因为我只能在Safari中重现它:

  1. 特定的自定义字体(Aqua,在其他自定义字体上,它不可重现);
  2. 特定的块大小(当有2行文本时)。
  3. 由于CORS问题,Chrome和Firefox并未真正加载字体:

    CORS

    但Safari可以,您可以在this JSFiddle上查看。

    此片段仅针对历史记录,因为您网站上的特定字体仅可通过HTTP使用(但SO片段需要HTTPS):

    &#13;
    &#13;
    function checkname(id) {
      var error = "";
      var value = document.getElementById(id).value;
      if (value.length < 1) {
        error = "Please fill in your (first)name.";
      }
      if (value.length > 50) {
        error = "Please fill in your (first)name. Your name is to long.";
      }
      if (value.indexOf("@") > -1) {
        error = "I think you are filling in your emailadress";
      }
      if (value.indexOf(".") > -1) {
        error = "Please check your (first)name, most names don't have a '.'.";
      }
      if (value.indexOf("/") > -1) {
        error = "Please check your (first)name, most names don't have a '/'.";
      }
      var matches = value.match(/\d+/g);
      if (matches != null) {
        error = "Please check your (first)name, we are all people, our (first)name doesn't contain a number.";
      }
      if (error.length > 1) {
        document.getElementById(id + "reaction").innerHTML = error;
      } else {
        document.getElementById(id + "reaction").innerHTML = "";
      }
    }
    &#13;
    @font-face {
      font-family: "Aqua";
      src: url(http://north-wind.be/css/aqua.ttf) format("truetype");
    }
    
    .input {
      width: 100%;
      overflow: auto;
      margin: 24px auto;
      font-family: Aqua, Arial;
    }
    
    .input input[type=text],
    .input input[type=email] {
      border: 1pt solid limegreen;
      width: 150px;
      border-radius: 8px 0px 0px 8px;
      height: 40px;
      margin: 0;
      float: left;
      font-size: 20px;
    }
    
    .input div {
      border-radius: 0px 8px 8px 0px;
      width: 350px;
      background: limegreen;
      color: white;
      height: 36px;
      float: left;
      padding: 4px 0;
      margin: 0;
      vertical-align: middle;
    }
    
    .input div p {
      vertical-align: middle;
      text-align: center;
      margin: 0 auto;
      border: none;
      padding: 0;
      width: 100%;
      height: 100%;
      font-size: 12px;
      color: white;
    }
    &#13;
    <h3 id="forms">Forms</h3>
    <div class="forms">
      <form>
        <div class="input">
          <input placeholder="name" name="name" type="text" oninput="checkname(this.id)" id="name">
          <div>
            <p id="namereaction" class="reaction"></p>
          </div>
        </div>
        <div class="input">
          <input type="submit" name="submit" value="submit">
        </div>
      </form>
    </div>
    &#13;
    &#13;
    &#13;

    所以,我只建议您使用one of force redraw/repaint hacks,例如:

    var element = document.getElementById(id + 'reaction');
    if (error.length > 1) {
      element.innerHTML = error;
    } else {
      element.innerHTML = '';
    }
    element.style.display = 'none';
    element.offsetHeight;
    element.style.display = '';
    

    The fixed JSFiddle