jQuery load html specific div into webpage

房东的猫 提交于 2019-12-06 14:11:26

问题


having a bit of a issue

got four links when clicked i want to to load in content from an external html page into a div called content which slides down and shows the content from the external html page, but I want to use the href from the links to load in specific div tags from the external html page.. I'm nearly there just missing the final part.. I cant seem to use my variable inside my jquery load statement..

this is what ive got so far..

    $("#services a").click(function(){
     var href = $(this).attr("href");
      console.log(href)
      $('#content').slideToggle(function(){
       $("#content").load("services.html" + href); 
      });
    return false;
    });

also if anyone could point me how to slide the content div back when selecting a different link and then show that content for that link.. would be great..

thanks guys..


回答1:


Supposing the href is correct and is the same as a selector, you're missing a space:

$("#services a").click(function(){
     var href = $(this).attr("href");
     $('#content').slideToggle(function(){
         $("#content").load("services.html " + href); 
                      //                  ^^^ space needed here 
     });
     return false;
});

this is because the correct syntax for load() is

$("#content").load("services.html #IdOfelementToLoad"); 
//    ^^                ^^       ^         ^^
//  container         filename  space    selector for filter

Documentation



来源:https://stackoverflow.com/questions/18673669/jquery-load-html-specific-div-into-webpage

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!