Html 从另一个页面加载多个内容块而不指定ID

Html 从另一个页面加载多个内容块而不指定ID,html,load,matching,Html,Load,Matching,我通过jquery.load事件将内容从一个页面加载到另一个页面。我现在是这样做的: $('#someDiv').load('/directory/file.html #someDiv'); 但是,如果我不必每次都指定div的名称,那就更好了。由于两个页面上的内容相同,有没有一种方法可以说,“在第1页上找到一个div,并将其内容加载到第2页上匹配的div中?”这样做的目的是不必在每次添加新内容时都更新脚本 提前感谢。经过大约一周的努力,我终于明白了。脚本如下: // Run this func

我通过jquery.load事件将内容从一个页面加载到另一个页面。我现在是这样做的:

$('#someDiv').load('/directory/file.html #someDiv');
但是,如果我不必每次都指定div的名称,那就更好了。由于两个页面上的内容相同,有没有一种方法可以说,“在第1页上找到一个div,并将其内容加载到第2页上匹配的div中?”这样做的目的是不必在每次添加新内容时都更新脚本


提前感谢。

经过大约一周的努力,我终于明白了。脚本如下:

// Run this function on divs with the class of include
$("div.include").each(function(){
    // load shared content file
    var pathname = "shared-content.html"; 

    //get the id value of each div on current page with class of include
    var contentID = ("#" + $(this).attr("id"));

    //find div with same id value in shared content file, then load its content
    $(contentID).load(pathname + " " + contentID);  
});
使用步骤:

  • 将此脚本加载到document.ready或页面内容后的脚本标记中
  • 在共享内容文件中,添加一些html并将其放置在具有唯一ID的div中
  • 通过添加在共享内容文件中使用的相同唯一ID,在任何页面(加载脚本的页面)上加载共享内容