我是Jquery的新手。我正在尝试获取内容并显示另一个HTML。
这是我的代码不起作用:
<!doctype html>
<html>
<head>
<script type="text/javascript" src="jquery-1.4.4.js"></script>
</head>
<body>
<div id="target">
click here to see test1.html
</div>
<script type="text/javascript">
$('#target').click(function() {
$.get('test1.html', function(data) {
$('.result').html(data);
alert('Load was performed.');
});
});
</script>
</body>
</html>
我的问题......我怎样才能使这个工作。我已经阅读了文档,但如果我必须使用一些外部脚本(PHP)来实现这一点,我就无法获得。
如果你能给我一些线索,我将非常感激。
更新
我已成功完成了获取内部网页的任务:
<!doctype html>
<html>
<head>
<script type="text/javascript" src="jquery-1.4.4.js"></script>
</head>
<body>
<div id="target">
click here to see teste1.html
</div>
<div id="result">
</div>
<script type="text/javascript">
$('#target').click(function() {
$.get('teste1.html', function(data) {
$('#result').html(data);
alert('Load was performed.');
});
});
</script>
</body>
</html>
主要目标是获取外部网页,但这不起作用:
$.get('http://www.google.com/index.html', function(data) {
$('#result').html(data);
alert('Load was performed.');
});
有关如何实现这一目标的一些线索?
最诚挚的问候。
答案 0 :(得分:1)
主要问题是你没有输出元素,你有$('.result')
,但你没有class="result"
元素来查找/放入内容。
另请注意,.innerHTML
(您最终使用的内容)因实施而异,这意味着文档的各种内容(<head>
,<script>
等)可能会有所不同将其插入页面中的元素时被剥离。