My question is how can I get the HTML of one page and store it in an object that I can later search through with jQuery methods to get elements by id, name,class, etc.
我的问题是,如何获得一个页面的HTML并将其存储在一个对象中,以便稍后使用jQuery方法搜索,以通过id、名称、类等获取元素。
This is what I have so far:
这是我目前所拥有的:
$(document).ready(function(){
$('#button').click(function() {
var page = $.ajax({
type: 'GET',
url: 'Grabber.php',
data: {url:$('#url')},
dataType: "HTML",
success: function(data){
alert(data); //this alert displays the correct information
}
}).responseText;
alert(page); //this alert displays nothing
});
});
How can i get that "page" variable to work? And even better, how can I store it so I can access it as if it were an HTML document. My only idea so far is as a DOM document.
如何让“page”变量发挥作用?更好的是,我如何存储它,使我可以像访问HTML文档一样访问它。到目前为止,我唯一的想法是作为DOM文档。
4 个解决方案
#1
2
$(document).ready(function(){
var page;
$('#button').click(function() {
$.ajax({
type: 'GET',
url: 'Grabber.php',
data: {url:$('#url')},
dataType: "HTML",
success: function(data){
populate(data);
}
});
function populate(a) {
page = a;
alert(page)
alert($(page).find('span').text())
}
});
});
Check the documentation, $.ajax
returns XMLHttpRequest
while the success method returns data, textStatus, XMLHttpRequest
. What you need here is the data
And to access it you can do something like:
检查文档,美元。ajax返回XMLHttpRequest,而success方法返回数据、textStatus和XMLHttpRequest。这里你需要的是数据,你可以这样做:
$(page).find('span').text()
#2
1
The data
in only available after a successful AJAX call. So any variable outside of this success function isn't necessarily set yet:
只有在成功的AJAX调用之后才可用。所以这个成功函数之外的任何变量都没有必要设置
$(document).ready(function(){
$('#button').click(function() {
$.ajax({
type: 'GET',
url: 'Grabber.php',
data: {url:$('#url')},
dataType: "HTML",
success: function(data) {
// keep working _within_ this function
// and call it "page" instead of "data" if you want
alert(data);
// and to find something within this data:
$(data).find('.some-class')... // etc
}
});
});
});
#3
0
In your success callback you should assign a variable outside of it's local scope so that when the function ends, the data is not lost. Then you can pass it as the second parameter to the jQuery method to have your jQuery selector act on the AJAX-fetched document instead of the Document.
在成功回调中,应该在变量的本地范围之外分配一个变量,以便当函数结束时,不会丢失数据。然后,您可以将它作为第二个参数传递给jQuery方法,让jQuery选择器对ajax获取的文档执行操作,而不是对文档执行操作。
var a_nasty_global_variable_you_should_put_in_a_better_place;
// most of your code…
success: function(data){
a_nasty_global_variable_you_should_put_in_a_better_place = data;
}
// a little later…
$('#button', a_nasty_global_variable_you_should_put_in_a_better_place).addClass('is-awesome');
You get the gist of it, no?
你明白了吧?
#4
0
You can either use global variable if you want that variable to be at same page where this ajax is present
如果希望该变量位于ajax所在的页面,则可以使用全局变量
or
或
You can use cookies to store your responseText if have to store some small amount of data and use responceText on other pages for cookies with jquery HELP?
如果需要存储少量数据,可以使用cookie来存储responseText,并在其他页面上使用responceText来存储jquery帮助下的cookie ?
#1
2
$(document).ready(function(){
var page;
$('#button').click(function() {
$.ajax({
type: 'GET',
url: 'Grabber.php',
data: {url:$('#url')},
dataType: "HTML",
success: function(data){
populate(data);
}
});
function populate(a) {
page = a;
alert(page)
alert($(page).find('span').text())
}
});
});
Check the documentation, $.ajax
returns XMLHttpRequest
while the success method returns data, textStatus, XMLHttpRequest
. What you need here is the data
And to access it you can do something like:
检查文档,美元。ajax返回XMLHttpRequest,而success方法返回数据、textStatus和XMLHttpRequest。这里你需要的是数据,你可以这样做:
$(page).find('span').text()
#2
1
The data
in only available after a successful AJAX call. So any variable outside of this success function isn't necessarily set yet:
只有在成功的AJAX调用之后才可用。所以这个成功函数之外的任何变量都没有必要设置
$(document).ready(function(){
$('#button').click(function() {
$.ajax({
type: 'GET',
url: 'Grabber.php',
data: {url:$('#url')},
dataType: "HTML",
success: function(data) {
// keep working _within_ this function
// and call it "page" instead of "data" if you want
alert(data);
// and to find something within this data:
$(data).find('.some-class')... // etc
}
});
});
});
#3
0
In your success callback you should assign a variable outside of it's local scope so that when the function ends, the data is not lost. Then you can pass it as the second parameter to the jQuery method to have your jQuery selector act on the AJAX-fetched document instead of the Document.
在成功回调中,应该在变量的本地范围之外分配一个变量,以便当函数结束时,不会丢失数据。然后,您可以将它作为第二个参数传递给jQuery方法,让jQuery选择器对ajax获取的文档执行操作,而不是对文档执行操作。
var a_nasty_global_variable_you_should_put_in_a_better_place;
// most of your code…
success: function(data){
a_nasty_global_variable_you_should_put_in_a_better_place = data;
}
// a little later…
$('#button', a_nasty_global_variable_you_should_put_in_a_better_place).addClass('is-awesome');
You get the gist of it, no?
你明白了吧?
#4
0
You can either use global variable if you want that variable to be at same page where this ajax is present
如果希望该变量位于ajax所在的页面,则可以使用全局变量
or
或
You can use cookies to store your responseText if have to store some small amount of data and use responceText on other pages for cookies with jquery HELP?
如果需要存储少量数据,可以使用cookie来存储responseText,并在其他页面上使用responceText来存储jquery帮助下的cookie ?