2016-01-28 219 views
0

我有一個非常基本的切換腳本,它工作正常,但我想切換標籤,以便它表明顯示第一則隱藏jQuery的切換標籤/按鈕上的文字

我的HTML是:

<button type="button" class="show">Show</button> 
<br /> 
<br /> 
<div id="info">#info</div> 
<div id="edit">#edit</div> 

和我jQuery是

$("#edit").hide(); 
$(".show").click(function(){ 
$("#info, #edit").fadeToggle("slow"); 
}); 

CSS是:

#info { 
position:absolute; 
left:0px; 
background-color:green; 
width:100px; 
height:100px; 
} 

#edit { 
position:absolute; 
background-color:red; 
width:100px; 
height:100px; 
} 
+1

你在哪裏把jQuery代碼,你把它包裝成$(文件)。就緒()? – Stickers

+0

不確定你的意思?內容在第一次加載時隱藏,但你想隱藏它? – Mushr00m

+0

'$(document).ready(){alert('當文檔被完全加載時運行這個代碼')}'。如果你的html元素上面有jQuery代碼,它們還不存在,所以你的jQuery不會執行。 – Xorifelse

回答

0

加入這一行:

$('button').text($('button').text() === 'Show' ? "Hide" : "Show"); 

那麼你的JavaScript是這樣的:

$("#edit").hide(); 
$("show").click(function(){ 
    $("#info, #edit").fadeToggle("slow"); 
    $('button').text($('button').text() === 'Show' ? "Hide" : "Show"); 
}); 

這條線切換依賴於當前值的按鈕上的文字。

此外,當切換時,您的彩色框會稍微移動一點,因爲left:0只在一個DIV上。

的jsfiddle:https://jsfiddle.net/xerbefmg/