浏览器不停止加载与jQuery $不用彷徨彷徨、加载、浏览器、jQuery

2023-09-10 20:12:53 作者:删了你网速明显见快了

我用下面的jQuery(1.4)code测试的cookies是否被接受:

I use the following jQuery (1.4) code to test whether cookies are accepted:

$.get("http://localhost:8080/cookietester/cookietester", function(data) {
    if (data == "false")
        document.write("Activate cookies!");
    else if(data == "true")
        document.write("ok");
});

不过,浏览器发出信号,它不会停止页面加载。谷歌浏览器5不执行该脚本正常,不显示任何内容。

But the browser signals that it doesn't stop page loading. Google Chrome 5 doesn't execute the script properly and doesn't display anything.

有什么问题与此code?

Is something wrong with this code?

推荐答案

您不能使用 < $ C C> $文件撰写 在那种情况下(其实我避免它只要有可能),而使用的 .append() 将消息添加到&LT;身体GT; ,像这样的:

You can't use document.write in that scenario (actually I avoid it whenever possible), instead use .append() to add a message to the <body>, like this:

$.get("http://localhost:8080/cookietester/cookietester", function(data) {
  if (data == "false")
    $(document.body).append("Activate cookies!");
  else if(data == "true")
    $(document.body).append("ok");
});

或者,你可以只是提醒它:

Or, you could just alert it:

$.get("http://localhost:8080/cookietester/cookietester", function(data) {
  if (data == "false")
    alert("Activate cookies!");
  else if(data == "true")
    alert("ok");
});
 
精彩推荐
图片推荐