如何编写HTML和Javascript代码。
我有两个文本内容,例如(嗨,你好,Hlo,你好吗?)(我是5n你呢?)。
当值到达3时,需要显示第一个内容(嗨,你好)。
如果不出现,则值3需要显示第二个内容(我5n,那你呢?)。
谢谢。
答案 0 :(得分:2)
您的问题过于广泛,难以理解,这个价值来自何处?这些文本是在同一元素中还是在两个不同元素中?您需要在这里显示的几种皮革的用途:
HTML:
<div id="when3" class="hide">Hi, hlo how are you</div>
<div id="not3" class="hide">l'm 5n what about you</div>
CSS:
/* Hide block */
.hide {
display: none
}
JS:
let value = 3; //use user input here
function show() {
if (value === 3) {
document.getElementById("when3").classList.remove("hide");
document.getElementById("not3").classList.add("hide");
} else {
document.getElementById("not3").classList.remove("hide");
document.getElementById("when3").classList.add("hide");
}
}
show();
HTML:
<div id="output"></div>
JS:
let value = 3; //use user input here
function show() {
if (value === 3) {
document.getElementById("output").innerHTML = "Hi, hlo how are you";
} else {
document.getElementById("output").innerHTML = "l'm 5n what about you";
}
}
show();
<div>
。用于导航。HTML:
<ul>
<li onclick="show('profile')">Profile</li>
<li onclick="show('friends')">Profile</li>
<li onclick="show('messages')">Profile</li>
</ul>
<div id="profile" class="hide">Profile Details</div>
<div id="friends" class="hide">Friends</div>
<div id="messages" class="hide">Messages</div>
CSS:
/* Hide block */
.hide {
display: none
}
JS:
function show(id) {
hideAll();
let element = document.getElementById(id);
element.classList.remove("hide");
}
function hideAll() {
document.getElementById("profile").classList.add("hide");
document.getElementById("friends").classList.add("hide");
document.getElementById("messages").classList.add("hide");
}
答案 1 :(得分:0)
HTML:
<div id="sample"></div>
JS:
var test = document.getElementById("sample");
var value;
if(value === 3){
test.innerHTML = "Hi, hlo how are you";
}
else{
test.innerHTML = "l'm 5n what about you";
}
答案 2 :(得分:0)
html
<input type="text" id="txt"/><br/>
<input type="button" name="go" onClick="result()" value="send"/><br/>
<div id="msg"> </div>
javascript
function result(){
var test = document.getElementById("msg");
var value=document.getElementById("txt");
if(value === 3)
{
test.innerHTML = "Hi, Hlo how are you";
}
else
{
test.innerHTML = "l'm 5n what about you";
}
}
答案 3 :(得分:0)
类似的事情应该起作用
function showText(val, id) {
document.getElementById(id).innerHTML = val == 3 ? "Hi, Hlo how are you" : "l'm 5n what about you";
}
showText(3, 'text');
showText(4, 'text1');
<div id="text"></div>
<div id="text1"></div>