在jQuery ajax调用以检索整个XHTML文档之后,从结果字符串中选择特定元素的最佳方法是什么?也许有一个库或插件可以解决这个问题?
如果W3C规范中的div通常允许,jQuery只能选择存在于字符串中的XHTML元素; 因此,我很好奇选择喜欢的东西<title>
,<script>
和<style>
.
根据jQuery文档:
http://docs.jquery.com/Core/jQuery#htmlownerDocument
HTML字符串不能包含div中无效的元素,例如html,head,body或title元素.
因此,既然我们已经确定jQuery没有提供这样做的方法,我将如何选择这些元素?例如,如果您可以告诉我如何选择远程页面的标题,那将是完美的!
谢谢,皮特
我正在尝试学习jQuery的ajax函数.我有它的工作,但jQuery没有在返回的HTML DOM中找到元素.在与jquery相同的文件夹中,运行此页面:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
<head>
<title>runthis</title>
<script type="text/javascript" language="javascript" src="jquery-1.3.2.min.js"></script>
<script tyle="text/javascript">
$(document).ready(function(){
$('input').click(function(){
$.ajax({
type : "GET",
url : 'ajaxtest-load.html',
dataType : "html",
success: function(data) {
alert( data ); // shows whole dom
alert( $(data).find('#wrapper').html() ); // returns null
},
error : function() {
alert("Sorry, The requested property could not be found.");
}
});
});
});
</script
</head>
<body>
<input type="button" value="load" />
</body>
</html>
Run Code Online (Sandbox Code Playgroud)
加载此页面"ajaxtest-load.html":
<!DOCTYPE html PUBLIC …
Run Code Online (Sandbox Code Playgroud)