Safari JS innerHTML bug/error

Safari JS innerHTML bug/error

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

网站预览:Preview

为了生成 error/bug 我在绿色框中的第三个 img 中做了这个问题: 填一个@ 添加号码 删除号码

这是我的 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="";
    }
}

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

  1. 特定的自定义字体(Aqua,在其他自定义字体上无法重现);
  2. 特定的块大小(当有 2 行文本时)。

Chrome 和 Firefox 由于 CORS 问题而没有真正加载字体:

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

下面的这段代码仅用于历史记录,因为您网站上的特定字体只能通过 HTTP 访问(但 SO 代码段需要 HTTPS):

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 = "";
  }
}
@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;
}
<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>

所以,我只能建议你使用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.