的情況是:我有幾個td
S和每個td
持有不同的文本。我通過通用選擇器將點擊事件綁定到td
。jQuery來改變基於當前文本的文本值在TD
當我點擊td
時,會出現一個下拉菜單,並且td
內的p
標籤中的文本將變爲「已打開」。當我再次點擊相同的td
(它將包含文本「打開」)時,下拉將隱藏並且文本將變回原始值。
我相信我們需要得到一些全局變量文本的值或者你有任何其他的想法去實現它?
的結構如下:
$(document).ready(function() {
$("#timeofday p").bind('click',function(event){
var read_txt= $(this).text();
$(this).text($(this).text() == 'opened' ? 'Click me' : 'Opened');
if(read_txt=='CLOSE')
{
$(this).parent().find(" > .dropdown").hide();
}
else
{
$("#timeofday .dropdown").not(this).slideUp();
$("#timeofday p").not(this).text($("#timeofday p").text('Click me'));
$(this).parent().find(" > .dropdown").slideToggle();
}
});
的HTML如下:
<table border="0" cellspacing="0" cellpadding="0">
<tr>
<td id="timeofday" class="avbltyltblue">
<p>Click me</p>
<div id="dropdown" class="dropdown">
<div style="padding:10px 0"><button class="up" style="width:100px">UP</button></div>
<ul class="tlist">
<li>1:00PM</li>
<li>2:00PM</li>
<li>3:00PM</li>
<li>4:00PM</li>
<li>5:00PM</li>
<li>6:00PM</li>
<li>7:00PM</li>
<li>8:00PM</li>
<li>9:00PM</li>
<li>10:00PM</li>
</ul>
<div style="padding:10px 0"><button class="down" style="width:100px">DOWN</button></div>
</div>
</td>
<td id="timeofday" class="avbltyltblue">
<p>test this</p>
<div id="dropdown" class="dropdown">
<div style="padding:10px 0"><button class="up" style="width:100px">UP</button></div>
<ul class="tlist">
<li>1:00PM</li>
<li>2:00PM</li>
<li>3:00PM</li>
<li>4:00PM</li>
<li>5:00PM</li>
<li>6:00PM</li>
<li>7:00PM</li>
<li>8:00PM</li>
<li>9:00PM</li>
<li>10:00PM</li>
</ul>
<div style="padding:10px 0"><button class="down" style="width:100px">DOWN</button></div>
</div>
</td>
</tr>
<tr>
<td id="timeofday" class="avbltyltblue" style="z-index:1">
<p>CLick again</p>
<div id="dropdown" class="dropdown">
<div style="padding:10px 0"><button class="up" style="width:100px">UP</button></div>
<ul class="tlist">
<li>1:00PM</li>
<li>2:00PM</li>
<li>3:00PM</li>
<li>4:00PM</li>
<li>5:00PM</li>
<li>6:00PM</li>
<li>7:00PM</li>
<li>8:00PM</li>
<li>9:00PM</li>
<li>10:00PM</li>
</ul>
<div style="padding:10px 0"><button class="down" style="width:100px">DOWN</button></div>
</div>
</td>
<td id="timeofday" class="avbltyltblue">
<p>mouse event</p>
<div id="dropdown" class="dropdown">
<div style="padding:10px 0"><button class="up" style="width:100px">UP</button></div>
<ul class="tlist">
<li>1:00PM</li>
<li>2:00PM</li>
<li>3:00PM</li>
<li>4:00PM</li>
<li>5:00PM</li>
<li>6:00PM</li>
<li>7:00PM</li>
<li>8:00PM</li>
<li>9:00PM</li>
<li>10:00PM</li>
</ul>
<div style="padding:10px 0"><button class="down" style="width:100px">DOWN</button></div>
</div>
</td>
</tr>
</table>
總之,如果我在「點擊我」下拉點擊會出現,當我再次點擊它,文本應該恢復到原始值,下拉菜單應該消失。
什麼是原文應該是?我如何使用數據函數? – 2012-02-20 17:40:43
「原始文本」只是一個標識,把它作爲一個變量的名字 - 它可以是任何你想要的。查看jQuery .data()api http://api.jquery.com/data/ – 2012-02-20 17:43:50