在Index.html中,我有:
<head>
<script>
$(function(){ $(".myDIV").load("page.htm?city=London"); });
</script>
</head>
<body>
<div class="myDIV"></div>
</body>除参数?city=London外,其他参数均可正常工作
我的意思是page.htm是在index.htm内部打开的,但是参数城市是不可见的。它应该起作用吗?
在page.htm中,我有
<script>
var getUrlParameter = function getUrlParameter(sParam) {
var sPageURL = decodeURIComponent(window.location.search.substring(1)),
sURLVariables = sPageURL.split('&'),
sParameterName,
i;
for (i = 0; i < sURLVariables.length; i++) {
sParameterName = sURLVariables[i].split('=');
if (sParameterName[0] === sParam) {
return sParameterName[1] === undefined ? true : sParameterName[1];
}
}
};
</script>最后
<div class="city"></div>
<script>
var cityVar= getUrlParameter('city');
$('.city').html(cityVar);
</script>发布于 2016-01-07 02:53:42
尝试将querystring作为第二个参数进行传递:
$(".myDIV").load("page.html", { city: 'London' });发布于 2016-01-07 03:00:46
我认为您的问题在于您试图使用javascript而不是在服务器端检索查询字符串。
服务器将看到您在load中传递的查询字符串,而javascript将看到当前的url。
例如,您可以简单地执行以下操作:
<script>
var city = "<?php echo $_GET['city']; ?>";
</script>https://stackoverflow.com/questions/34640191
复制相似问题