怎么讓我的p2自動在one和two這兩個樣式之間自動跳轉(zhuǎn)呢,有不有大神可以幫個忙,謝謝
<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>className屬性</title>
<style>
? ? body{ font-size:16px;}
? ? .one{
border:10px solid #cab;
width:230px;
height:50px;
background:#ccc;
color:red;
? ? }
.two{
border:10px solid #ccc;
width:230px;
height:50px;
background:#9CF;
color:blue;
}
</style>
</head>
<body>
? ? <p id="p1" > JavaScript使網(wǎng)頁顯示動態(tài)效果并實現(xiàn)與用戶交互功能。</p>
? ? <input type="button" value="添加樣式" onclick="add()"/>
<p id="p2" class="one">JavaScript使網(wǎng)頁顯示動態(tài)效果并實現(xiàn)與用戶交互功能。</p>
? ? <input type="button" value="更改外觀" onclick="modify()"/>
<script type="text/javascript">
? ?function add(){
? ? ? var p1 = document.getElementById("p1");
? ? ? p1.className="one";
? ?}
? ?
? ?var p2 = document.getElementById("p2");
? ? ? p2.className="two";
? ? ? document.write("現(xiàn)在的樣式ID是:"+p2.className);
? ?function modify(){
? ? ? ? if(p2.className=="one")
? ? ? ? {
? ? ? ? ? ? p2.className="two"
? ? ? ? }
? ? ? ? else
? ? ? ? {
? ? ? ? ? ? p2.className="one"
? ? ? ? }
? ? }
? ??
? ?
</script>
</body>
</html>
2018-08-13
自動切換的話需要使用到定時器。setInterval(),js定時器相關(guān)知識可以網(wǎng)上搜索一下,good luck
<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>className屬性</title>
<style>
? ? body{ font-size:16px;}
? ? .one{
border:1px solid #eee;
width:230px;
height:50px;
background:#ccc;
color:red;
? ? }
.two{
border:1px solid #ccc;
width:230px;
height:50px;
background:#9CF;
color:blue;
}
</style>
</head>
<body>
? ? <p id="p1" > JavaScript使網(wǎng)頁顯示動態(tài)效果并實現(xiàn)與用戶交互功能。</p>
? ? <input type="button" value="添加樣式" onclick="add()"/>
<p id="p2" class="one">JavaScript使網(wǎng)頁顯示動態(tài)效果并實現(xiàn)與用戶交互功能。</p>
? ? <input type="button" value="更改外觀" onclick="modify()"/>
? ? <input type="button" value="自動切換" onclick="start()">
? ? <input type="button" value="停止" onclick="stop()"/>
? ??
<script type="text/javascript">
? ?function add(){
? ? ? var p1 = document.getElementById("p1");
? ? ? p1.className='one';
? ?}
? ?function modify(){
? ? ? var p2 = document.getElementById("p2");
? ? ? p2.className=p2.className==='two'?'one':'two';
? ?}
? ?var init;
? ?
? ?function start(){
? ? ? ?init = self.setInterval('modify()',1000);
? ?}
? ?
? ?function stop(){
? ? ? ?init = self.clearInterval(init);
? ?}
??
</script>
</body>
</html>
2018-09-07
我想問一下這個
? ? ? p2.className=p2.className==='two'?'one':'two';
這個是什么意思?
2018-08-03
<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>className屬性</title>
<style>
body{ font-size:16px;}
.one{
border:10px solid #cab;
width:230px;
height:50px;
background:#ccc;
color:red;
}
.two{
border:10px solid #ccc;
width:230px;
height:50px;
background:#9CF;
color:blue;
}
</style>
</head>
<body>
<p id="p1" > JavaScript使網(wǎng)頁顯示動態(tài)效果并實現(xiàn)與用戶交互功能。</p>
<input type="button" value="添加樣式" onclick="add()"/>
<p id="p2" class="one">JavaScript使網(wǎng)頁顯示動態(tài)效果并實現(xiàn)與用戶交互功能。</p>
<input id="sh1" type="button" value="one" onclick="modify()"/>
<script type="text/javascript">
function add(){
var p1 = document.getElementById("p1");
p1.className="one";
}
var p2 = document.getElementById("p2");
var button = document.getElementById("button");
// ? p2.className="two";
// ? document.write("現(xiàn)在的樣式ID是:"+p2.className);
function modify(){
if(p2.className=="one")
{
p2.className="two";
document.getElementById("sh1").value="two";
}
else
{
p2.className="one";
document.getElementById("sh1").value="one";
}
}
</script>
</body>
</html>
樓主寫的已經(jīng)可以更換樣式了,我調(diào)整了一下button的問題