[英]JavaScript including another file
I have this bit of code: 我有这段代码:
var curState = 'Agenda';
function switchState(newState) {
curState = newState;
}
function Inbox() {
switchState('Inbox');
document.getElementById("u-content").innerHTML = "Currently, you're viewing the Inbox.";
}
function Friends() {
switchState('Friends');
document.getElementById("u-content").innerHTML = "Currently, you're viewing the Friends.";
}
function Agenda() {
switchState('Agenda');
document.getElementById("u-content").innerHTML = "Currently, you're viewing the Agenda.";
}
function List() {
switchState('List');
document.getElementById("u-content").innerHTML = "Currently, you're viewing the Book List.";
}
function News() {
switchState('News');
document.getElementById("u-content").innerHTML = "Currently, you're viewing the News.";
}
function Notes() {
switchState('Notes');
document.getElementById("u-content").innerHTML = "Currently, you're viewing the Notes.";
}
Of course the text displayed is a test and I will change it. 当然,显示的文字是测试,我将对其进行更改。 The point is, I'd have to put a lot of text into these 'statuses' of a menu and to simply type it out in here is not very handy, especially if there are changes to be made later on.
关键是,我必须在菜单的这些“状态”中放入很多文本,并在此处简单地键入不是很方便,特别是如果以后需要进行更改时。 Therefore I'd like to know how to add different forms of files in JavaScript, or let them display using JavaScript, like a *.php file.
因此,我想知道如何在JavaScript中添加不同形式的文件,或者让它们使用JavaScript显示,例如* .php文件。
Is there somebody who knows how to do such a thing? 是否有人知道该怎么做? I've searched quite a lot on the internet, but was unable to find something useful to me, except
document.getElementById().innerHTML = "";
我已经在互联网上进行了大量搜索,但是找不到对我有用的东西,除了
document.getElementById().innerHTML = "";
There are three easy ways to do this: 有三种简单的方法可以做到这一点:
Variant A: Get your innerHTML from the server via an AJAX call - something like 变体A:通过AJAX调用从服务器获取您的innerHTML-类似
function News() {
switchState('News');
document.getElementById("u-content").innerHTML = "Please wait for News to load.";
//start AJAX call here
}
function AJAXcallOnSuccess() {
document.getElementById("u-content").innerHTML = //Result text from AJAX call;
}
Variant B: Have your text delivered in the original page, but not shown. 变体B:在原始页面中传递了您的文本,但未显示。 Something like
就像是
<blah>
...
<div style="display: none;" id="newsPlaceHolder">
This is the text for the news item
</div>
...
</blah>
function News() {
switchState('News');
document.getElementById("u-content").innerHTML = document.getElementById("newsPlaceHolder").innerHTML
}
Variant B leads to the easiest Variant C: Have all DIVs ready in the page, but only show one at a time. 变体B导致最简单的变体C:在页面中准备好所有DIV,但一次只显示一个。
var activeDiv = null;
function News() {
switchState('News');
if (activeDiv) activeDiv.style.display='none';
activeDiv=document.getElementById("u-content-news");
activeDiv.style.display='block';
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.