這是我需要的東西得到完成:負荷的html頁面不刷新,當該div內的鏈接被點擊
基本上我有一個「地圖」,上面有一些「點」;當一個點被點擊,我想加載另一個圖像,而不是刷新頁面上的其他點(點是鏈接)。這樣做的一種方式是獲取div的顯示:無,點擊時,隱藏()當前圖像(以及點)和淡入淡出(),但我不想每次都加載所有圖像該頁面被訪問。
問題:
當與普通的PHP做它,它工作完全正常:當它是「main.php」它打開圖像「1-25.png」,當鏈接(點)爲點擊它打開'main.php?m = mapname'並打開'mapname.png'。但是,我希望頁面保持靜止,並且更改內容(不刷新)。
我有一些導航按鈕的第一頁,另一個帶鏈接的頁面加載的div。我需要在點擊鏈接時更改div內的內容,而不刷新瀏覽器。如果我使用div外的任何鏈接或按鈕,它會很好,但只要我嘗試在div內導航,它就不起作用。
這裏是一些代碼: 在index.php
<a href="1.php">First</a><br>
<a href="2.php">Second</a><br>
<a href="3.php">Third</a><br>
<?PHP
include ('div.php');
?>
的div.php
<div id="map"></div>
<script>
function getQueryVariable(variable)
{
var query = window.location.search.substring(1);
var vars = query.split("&");
for (var i=0;i<vars.length;i++) {
var pair = vars[i].split("=");
if(pair[0] == variable){return pair[1];}
}
return(false);
}
$(document).ready(function() {
$('#map').load('content.php');
$('a').click(function() {
alert(getQueryVariable("m"));
$.ajax({
type : 'GET',
url : 'content.php',
data: 'm=' + getQueryVariable("m"),
success : function(html) {
$("#map").html(html);
}
});
});
});
</script>
和content.php
if (!isset($_GET['m']) || empty($_GET['m'])) {
echo "<div id=\"map1\" style=\"position: relative; left: 0; top: 0;\">
<img src=\"./inc/pics/1-25.png\" style=\"position: relative; top: 0; left: 0; max-height: 500; max-width: 700;\">
<a id=\"1\" href=\"index.php?m=l50\"><img src=\"./inc/pics/reddot.png\" style=\"position: absolute; top: 180; left: 180;\"></a>
<a id=\"2\" href=\"index.php?m=r50\"><img src=\"./inc/pics/reddot.png\" style=\"position: absolute; top: 280; left: 410;\"></a>
</div></div>";
} else if ($_GET['m'] == 'l50') {
echo "<div id=\"map2\" style=\"position: relative; left: 0; top: 0;\">
<img src=\"./inc/pics/2-50-left.png\" style=\"position: relative; top: 0; left: 0; max-height: 500; max-width: 700;\">
<a id=\"3\" href=\"index.php?m=l25\"><img src=\"./inc/pics/reddot.png\" style=\"position: absolute; top: 160; left: 110; \"></a>
<a id=\"4\" href=\"index.php?m=tl25\"><img src=\"./inc/pics/reddot.png\" style=\"position: absolute; top: 140; left: 350;\"></a>
<a id=\"5\" href=\"index.php?m=bl25\"><img src=\"./inc/pics/reddot.png\" style=\"position: absolute; top: 350; left: 350;\"></a>
</div></div>";
} else if ($_GET['m'] == 'r50') {
echo "<div style=\"position: relative; left: 0; top: 0;\">
<img src=\"./inc/pics/3-50-right.png\" style=\"position: relative; top: 0; left: 0; max-height: 500; max-width: 700;\" >
<a id=\"6\" href=\"index.php?m=t25\"><img src=\"./inc/pics/reddot.png\" style=\"position: absolute; top: 150; left: 50;\"></a>
<a id=\"7\" href=\"index.php?m=b25\"><img src=\"./inc/pics/reddot.png\" style=\"position: absolute; top: 380; left: 70;\"></a>
</div>";
}
UPDATE \修復
我刪除了 $('#map').load('content.php');
從div.php,然後改變了點擊事件
$('#map').on('click', 'a#1', function(event) {
$("#map").load('map.php?m=l50');
event.preventDefault();
});
您能否提供一個鏈接到實時頁面?我很難想象這些視覺效果。 –
它在我的本地主機上。 – AlexCE
你想看看AJAX。 –