我尝试根据选择框中的选择将内容加载到 Intex 中的 div 中。它不起作用,所以出了问题。我制作了一个包含四个页面的示例来基本展示它是如何的:
Index.html
one.html
two.html
three.html
在索引中,我有一个 ID 为“selectchoice”的选择元素:
<select id="selectchoice>
<option>Select a choice</option>
<option>1</option>
<option>2</option>
<option>3</option>
</select>
我在 Index 中还有一个 id 为“get_content”的 div:
<div id="get_content"></div>
当我将 select 元素更改为选项 1、2 或 3 时,我想将 one.html、two.html 或 Three.html 加载到 div get_content 中。
然后我将此代码放在 header if Index.html 中,位于 jQuery 文件链接之后。
<script>
$("#selectchoice").change(function(){
$("#get_content").load("");
$("#get_content").load("one.html");
$("#get_content").load("two.html");
$("#get_content").load("three.html");
});
然后我运行该站点(在具有在同一站点上运行的其他加载脚本的服务器上),但它不起作用。怎么了? :/
对脚本和编程有点陌生,所以如果有任何标准错误,请不要感到惊讶。
有人发现错误了吗?
最佳答案
首先应关闭 select 标签的 id 属性,并优化您的 javascript 函数。类似的东西
<script>
$("#selectchoice").change(function(){
var page = this.selectedIndex;
if(page == 1) {
$("#get_content").load("one.html");
return;
}
else if(page == 2) {
$("#get_content").load("two.html");
return;
}
....
});
关于javascript - 从同一网站的外部页面加载 div 中的内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13953700/