简体   繁体   English

Javascript中的引用问题

[英]Quotes issue in Javascript

I want to refresh an img element within a div every 5 seconds that would indicate whether the user is connected to the internet or not connected. 我想每隔5秒在div中刷新一个img元素,指示用户是连接到互联网还是未连接。 If the user is connected, it will display an image online, but if the user is offline, a local image will display. 如果用户已连接,则会在线显示图像,但如果用户处于脱机状态,则会显示本地图像。

However, I cannot get around this quote problem. 但是,我无法解决这个引用问题。 I even tried the "&-quot;" 我甚至试过“&-quot;” thing (without the dash) and it still will not work. 事情(没有破折号),它仍然无法正常工作。 Here is the code: 这是代码:

<script type="text/javascript">
    window.setInterval("refreshDiv()", 5000);
    function refreshDiv(){
    document.getElementById("test").innerHTML = "<img id="this" src="http://stallionware.weebly.com/uploads/3/0/4/3/30431988/4901948_orig.png" onerror="this.src='nowifi.png'">";
}
</script>
<div id=wifi>
<img id="this" src="http://stallionware.weebly.com/uploads/3/0/4/3/30431988/4901948_orig.png" onerror="this.src='nowifi.png'">
</div>

I do not have the desire to use jQuery or Ajax or any server-side languages because this is all on a local machine. 我不想使用jQuery或Ajax或任何服务器端语言,因为这都在本地机器上。

You can escape those double quotes like this: 你可以逃避这样的双引号:

document.getElementById("this").innerHTML = "<img id=\"this\" src=\"http://stallionware.weebly.com/uploads/3/0/4/3/30431988/4901948_orig.png\" onerror=\"this.src='nowifi.png'\">";

By typing \\" you're telling the JavaScript that you want to insert a double quote inside your string. The other option you have is to use single quotes instead. (I've changed your getElementById from 'test' to 'this' since your HTML has no 'test' as Id) 通过键入\\"你告诉JavaScript你要在你的字符串中插入一个双引号。另一个选择是使用单引号。(我已经将你的getElementById从'test'改为'this',因为您的HTML没有'测试'作为Id)

Also, you shouldn't call a function using a string as you did on refreshDiv . 此外,您不应该像在refreshDivrefreshDiv使用字符串调用函数。 Instead you should call its name like this: 相反,你应该这样称呼它的名字:

window.setInterval(refreshDiv, 5000);

You should use a hierarchy of single quotes and double quotes. 您应该使用单引号和双引号的层次结构。 Using double quotes under double quotes makes the JS think is a combination of strings. 在双引号下使用双引号使JS认为是字符串的组合。 But it fails without the operators. 但没有运营商就失败了。

<script type="text/javascript">
    window.setInterval("refreshDiv()", 5000);
    function refreshDiv(){
    document.getElementById("test").innerHTML = '<img id="this" src="http://stallionware.weebly.com/uploads/3/0/4/3/30431988/4901948_orig.png" onerror="this.src='nowifi.png'">';
}
</script>
<div id=wifi>
<img id="this" src="http://stallionware.weebly.com/uploads/3/0/4/3/30431988/4901948_orig.png" onerror="this.src='nowifi.png'">
</div>

Apart from that rather than checking every 5 seconds I would suggest going and using online and offline events of browser - which then saves your code from having setInterval kind of stuff. 除了那个,而不是每5秒检查一次,我建议去使用浏览器的在线和离线事件 - 然后保存你的代码,使其不具备setInterval类的东西。

In JavaScript, you can use single-quotes or double-quotes. 在JavaScript中,您可以使用单引号或双引号。 For your case, you should put the string containing the div in single-quotes and have the HTML attributes within that string using double-quotes. 对于您的情况,您应该将包含div的字符串放在单引号中,并使用双引号在该字符串中包含HTML属性。 However, it looks like you have a few other mistakes in your code anyway. 但是,无论如何,您的代码中似乎还有其他一些错误。 Try this: 试试这个:

<img id="image" src="http://stallionware.weebly.com/uploads/3/0/4/3/30431988/4901948_orig.png" onerror="error">

with the following in your Javascript: 在您的Javascript中有以下内容:

var theImage = document.getElementById('image');
function error() {
  theImage.src='nowifi.png';
}

function refreshDiv(){
  theImage.innerHTML = '<img id="image" src="http://stallionware.weebly.com/uploads/3/0/4/3/30431988/4901948_orig.png"     
  onerror="error">';
}
setInterval(refreshDiv, 5000);

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

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