Using jQuery Load to get certain part of a page

≡放荡痞女 提交于 2019-12-10 12:05:44

问题


All, I'm using Wordpress so I know the main content falls within the following div:

<div id="content" role="main">

I'm trying to use the jQuery load function to display only the content in between those tags. How can I say the id of content but the role of main?

I tried to do the following:

page_url = $(this).attr("href");
$("#menu_content").load(page_url + ' #content main');

This doesn't display anything though. Any ideas on how to do this? Thanks!


回答1:


Try this:

 $("#menu_content").load(page_url + ' #content[role="main"]');

Am using an Attribute selector but only sense of using this would be if you have different "role" under different circumstances but it really makes no sense adding anything to an ID selector

http://api.jquery.com/category/selectors/

If you want only what is inside #content use $.get

  $.get(page_url, function(data){
   $("#menu_content").html( $(data).find('#content').html());
  })



回答2:


Load the page first and then grab the content:

$('<div/>').load(page_url , function() {
    $('#menu_content').html($(this).find('#content').html());
});


来源:https://stackoverflow.com/questions/9713022/using-jquery-load-to-get-certain-part-of-a-page

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