Javascriptを切り替え
-
12-09-2019 - |
質問
思い切り替え効果がないかでにお越しいただきます。(i jave Jquery込).
とhtmlと同様
<table class="left-dates">
<tr><td>All Dates</td></tr>
<tr><td>01 dec 2009</td></tr>
<tr><td>02 dec 2009</td></tr>
<tr><td>03 dec 2009</td></tr>
<tr><td>04 dec 2009</td></tr>
</table>
<div class="box 01-dec-2009">
foo
</div>
<div class="box 03-dec 2009">
bar
</div>
<div class="box 04-dec-2009">
foobar
</div>
歩み
- すべての部の表示
- クリックtd><td>日まで非表示になりの部のクラスの日をクリック
- "をクリックすべての日"のショーもも
うどんこ療を綺麗に?理想的にはJQueryを使った.
解決
ここでjQueryを使って作業例です。
私は、ラベルはクラス名と同等になるように空白を削除するには、あなたのdivクラスとtd
のラベルを変更しなければならなかったことに注意してください。あなたがラベルでダッシュをしたくなかった場合は、ホワイトスペースを削除したり、それらに対応するdiv要素としてtd
s、同じクラス名を与え、その後、むしろ、その内、テキストよりクリックtd
のクラス名を見るためにJavaScriptで文字列操作を行うことができます。
<html>
<head>
<title>jQuery hiding example</title>
<script type='text/javascript' src='http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js'></script>
<script type='text/javascript'>
$(document).ready(function(){
$('td').click(function() {
var target = $(this).text();
if (target == 'All Dates') {
$('div.box').show();
} else {
$('div.box').hide();
$('div.' + target).show();
}
});
});
</script>
</head>
<body>
<table class="left-dates">
<tr><td>All Dates</td></tr>
<tr><td>01-dec-2009</td></tr>
<tr><td>02-dec-2009</td></tr>
<tr><td>03-dec-2009</td></tr>
<tr><td>04-dec-2009</td></tr>
</table>
<div class="box 01-dec-2009">
foo
</div>
<div class="box 03-dec-2009">
bar
</div>
<div class="box 04-dec-2009">
foobar
</div>
</body>
</html>
他のヒント
いうこのようになっております:
var $boxes = $('div.box');
$('.left-dates td:gt(0)').click(function(e){
var class = this.innerHTML.replace(/ /g, '-'); // Convert text to class
$boxes.filter(':visible').not('.' + class).hide(); // All visible div.box that don't have the class we are going to show.
$boxes.filter('.' + class).show(); // Show this class
});
$('.left-dates td:first').click(function(e){
$boxes.show();
});
編集: 入れ替え .click()
のための .live('click')
.場合、あなたのために、トン行すると良いのかを使用 .live()
の代わりに結合する click()
イベントを各 td
また、HTMLだ投稿がエラーとなります。の 03
divが抜けてハイフンの前に 2009
一意に<td>All Dates</td>
を識別します。すべてのあなたの日付<td>s
に同じクラスまたはいくつかの他の識別子を割り当てます。それらを同じ日付と1以外のすべての■は要素を隠しクリックハンドラを与えます。あなたの例では、あなたは私がやる何のために必要がありますあなたのdivの日付、クラス名と同じ<td>
に日付を作ると一致していないよ。
<table class="left-dates">
<tr><td id="alldates">All Dates</td></tr>
<tr><td id="date">01 dec 2009</td></tr>
<tr><td id="date">02 dec 2009</td></tr>
<tr><td id="date">03 dec 2009</td></tr>
<tr><td id="date">04 dec 2009</td></tr>
</table>
// unhide all box elements
$("#alldates").click(function(e){ $(".box").show(); });
// For each date <td> element
$("#date").each(function(){
// assign a click event
$(this).click(function(e){
// when the user clicks, get the
// <td>'s text contents
var myval = $(this).text();
// and grab each element with the
// 'box' css class
$(".box").each(function(){
// for each of these 'box' elements,
// if it has a class matching the date
// they selected, return
if($(this).has(myval))
{
return;
}
else
{
// otherwise, hide itself
$(this).hide();
}
});
});
});
所属していません StackOverflow