-7
<script>
$(document).ready(function(){
   $('one').click(function(){
      if( loaded('new.html') ) {
        alert('yes');
      });
   });
});
</script>

有什么明确的方法可以让它工作吗?

4

2 回答 2

2

你在这里有一些普遍的误解。我将从代码中的语法/语义错误开始:

$('one')可能应该#one.one取决于您的元素是否具有 id 或类。一个if条件只需要一个关闭},你不小心添加了一个)。如果您在浏览器中按F12(或ctrl++ shifti,您将看到一个控制台,它会告诉您代码中是否存在错误。

然后不清楚你想做什么。

通常,当您要检查时,如果您的 html 已准备好,使用该ready方法(就像您已经做过的那样)就足够了。你不需要做任何额外的检查。

$(document).ready(function(){
    /* inside here, you can be sure your html is ready*/
});

现在这取决于您的代码loaded('new.html')到底在做什么。但是如果你new.html通过 ajax 加载,你应该使用成功回调来处理结果。有关这方面的更多信息,请参阅jQuery 。get()

通常,您要在new.html加载后执行的所有代码都应放入回调中。

var jqxhr = $.get("new.html", function() {
    alert("YES! Your html has successfully loaded");
    /* Put your code here*/
})
于 2013-07-08T08:22:06.097 回答
0

尝试使用 $.get() 函数来加载页面。 http://api.jquery.com/jQuery.get/

看看你想要什么,试试这个:

var isLoaded=false;

$(document).ready(function() {
   $.get('one.html', function(data) {
     isLoaded=true;
   });

   $('one').click(function(){
      if(isLoaded) {
        alert('yes');
      });
   });
});
于 2013-07-08T08:10:57.997 回答