我的任务是这样做,这样当我按下运行时,2个文本切换,所以“标题”文本出现在“新闻”中,反之亦然。
var headline = document.getElementById("headline");
var headline2 = "Student Wins Lottery";
headline.innerHTML = headline2;
var news = document.getElementById("news");
var news2 = "A student has won a million pounds";
news.innerHTML = news2;
我只是堆着你如何让它们转换,任何帮助和解释都会很棒,欢呼。
答案 0 :(得分:0)
可能的方法:
var headline = document.getElementById("headline"),
headline2 = document.getElementById("news"),
str1 = "Student Wins Lottery",
str2 = "A student has won a million pounds",
bool = true;
function change() {
if (!bool) {
headline.innerHTML = str1;
news.innerHTML = str2;
bool = true;
} else {
headline.innerHTML = str2;
news.innerHTML = str1;
bool = false;
}
}

<h1 id="headline">Student Wins Lottery</h1>
<div id="news">A student has won a million pounds</div>
<button onclick="change()">change heading</button>
&#13;
答案 1 :(得分:0)
尝试以下方法:
var headlineEl = document.getElementById("headline")
var newsEl = document.getElementById("news");
var headline = "Student Wins Lottery";
var news = "A student has won a million pounds";
var flag=true;
function changeText(){
if(flag){
headlineEl.innerHTML = news;
newsEl.innerHTML = headline;
flag = false;
}
else{
headlineEl.innerHTML = headline;
newsEl.innerHTML = news;
flag = true;
}
}
<h1 id="headline">Student Wins Lottery</h1>
<div id="news">A student has won a million pounds</div>
<input type="button" value="Run" onclick="changeText()" />