我正在尝试运行此代码,但它破坏了我的整个页面。该页面被标记为:

<script type="text/javascript">
$(document).ready(function() {
  $("#right_column h1").each( function() {
   var link = $(this).html()+"<br />";
   document.write(link);
});
});
</script>
<div id="right_column">
<h1>Company Profile</h1>
blablablablabla<br />

<h1>Nulla turpis nunc, dapibus ultricies.</h1>
blablablabla<br />
<h1>Pellentesque habitant morbi tristique proin laoreet.</h1>
blablablabla<br />


当我尝试运行代码时,它仅向我显示3个h1的内容,页面的其余部分(h1本身)不再加载。当我删除$(document).ready()函数,并将脚本块放在所有内容之后时,它工作得很好。

最佳答案

阅读文档后请勿使用document.write

如果要追加到页面,请执行以下操作:

  $(document).ready(function() {
     $("#right_column h1").each( function() {
       var link = $(this).html()+"<br />";
       $(document.body).append($(link));
     });
   });

关于jquery - 文件准备就绪错误,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/16067760/

10-13 07:58
查看更多