jQuery load with variable instead of element id
i've got a slight jQuery problem. What i want is to load a particular area of an external html doc into a div, instead it loads the whole document, not the specified area.
This is the trigger:
$('a').click(function(){ // my link to trigger the load
var pid = $(this).attr('href'); //the pid is the links href
getproject(pid); //trigger the load function, pass variable
});
This is the triggered function:
function getproject(pid) {
$('#container').load('data.html', pid);
}
So when i click my link, it should load the element with the id (#) specified by the link into my container, but it loads the whole data... i cant seem to find a solution to this.
The Link looks like this (cant use exact markup here): a href="#elementtoload"
The data document looks like this: div id="elementtoload" div id="..."
and loads of more elements with content, which should be load开发者_StackOverflowed by id from the links href.
Looking at the documentation for $ load, you should be able to do this:
function getproject(pid) {
$('#container').load('data.html #' + pid);
}
http://api.jquery.com/load/
Second section, Loading Page Fragments. For us to say exactly how this is relevant, you'd need to provide an example of the triggering link, and ideally the document you're loading.
Not sure what your actual intent is but it seems to me that you are going to alot of trouble to get the pid but then not using it in your load routine. I'm guessing the code should look more like this:
function getproject(pid) {
$(pid).hide().load('data.html');
}
String concatenation:
.load('data.html ' + pid);
Regarding your update:
<div id="#elementtoload">
should be <div id="elementtoload">
I was trying something similar and after some hours (...) michael came finally with something that works :) this did the trick for me, the #
should be in the url string, like this:
$('#div1').load('data.html #' + x);
精彩评论