【问题标题】:how to apply jquery on the content of html which is loaded in another page?如何在另一个页面中加载的html内容上应用jquery?
【发布时间】:2015-01-26 17:23:59
【问题描述】:

我有两个页面 index.html 和 header.html

index.html

<body>
  <div id="tpHeader"></div>
  <script>
    $(function(){
      $("#tpHeader").load("header.html");
    });
  </script>
</body> 

header.html

<div id="con1">
content 1
</div>
<div id="con2">
content 2
</div>

所以..当我在浏览器中查看我的文件 index.html 时,它看起来像

<div id="tpHeader">
  <div id="con1">
    content 1
  </div>
  <div id="con2">
    content 2
  </div>
</div>

现在我想在 index.html 中使用一些 js 或 jquery 来隐藏#con2 换句话说..我想在 index.html 中编写一些代码,这使得具有 id="con2" 的元素隐藏

我试过了

index.html

<body>
  <div id="tpHeader"></div>
  <script>
    $(function(){
      $("#tpHeader").load("header.html");
      $("#con2").hide(); // but this is not working 
    });
  </script>
</body> 

我也试过

document.getElementById("con2").style.display = "none";

他们都没有工作..请帮助我

【问题讨论】:

    标签: jquery html apply loaded


    【解决方案1】:

    使用.load( url [, data ] [, complete ] )方法完成功能

    请求完成时执行的回调函数。

    代码

    $("#tpHeader").load("header.html", function() {
        $("#con2").hide(); 
    });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-03-14
      • 2011-05-05
      • 1970-01-01
      • 1970-01-01
      • 2011-06-01
      • 1970-01-01
      • 2022-01-09
      • 1970-01-01
      相关资源
      最近更新 更多