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