[英]jquery switch between two views
I got two divs with the id's today
and tomorrow
. today
和tomorrow
我有两个div。 As only one of them can be shown, I wrote a javascript function which switches between those two. 由于只能显示其中之一,因此我编写了一个javascript函数在这两个函数之间切换。
function switchDay(selector) {
if (selector == "tomorrow") {
$("#today").hide();
$("#tomorrow").show();
$("#daySelector").html('<a href="#" onclick="return switchDay(\'today\');">this day</a> | next day');
}
if (selector == "today") {
$("#tomorrow").hide();
$("#today").show();
$("#daySelector").html('this day | <a href="#" onclick="return switchDay(\'tomorrow\');">next day</a>');
}
return false;
}
In my PHP I echo the switch links like this: 在我的PHP中,我像这样回显切换链接:
echo '<p id="daySelector">today | <a href="#" onclick="return switchDay(\'tomorrow\');">tomorrow</a></p>';
As you can see, I already switched hide() and show() to jquery functions (before I was using .style.display functions) and would now like to also ditch the old onclick
and rather use the jquery .click()
. 如您所见,我已经将hide()和show()切换为jquery函数(在使用.style.display函数之前),现在还希望放弃旧的
onclick
,而是使用jquery .click()
。 Though, I am not sure how I would change the switch links. 不过,我不确定如何更改开关链接。
How can I do this? 我怎样才能做到这一点? (Best would be if it didn't make my script bigger by much...)
(最好是如果它不能使我的脚本大很多……)
There are many approaches to this (God, I love programming because of this). 有很多解决方法(上帝,因此我喜欢编程)。
An easy one would be to do this: 一个简单的方法是:
$('#daySelector a').live('click', function () {
if ($(this).hasClass("tomorrow")) {
$("#today").hide();
$("#tomorrow").show();
$("#daySelector").html('<a href="#" class="today">this day</a> | next day');
}
if ($(this).hasClass("today")) {
$("#tomorrow").hide();
$("#today").show();
$("#daySelector").html('this day | <a href="#" class="tomorrow">next day</a>');
}
return false;
});
Then just do the PHP like this: 然后像这样做PHP:
echo '<p id="daySelector">today | <a href="#" class="tomorrow">tomorrow</a></p>';
I didn't test it. 我没有测试。 Should still work.
应该还是可以的。
Following a comment below that reminded me of live being deprecated. 在下面的评论之后,让我想起了直播被弃用的情况。 Here's how it would be using .on method.
这就是使用.on方法的方式。 I edited too avoiding usage of document for the binding.
我也进行了编辑,避免使用文档进行绑定。
$('#daySelector').on('click', 'a', function () {
if ($(this).hasClass("tomorrow")) {
$("#today").hide();
$("#tomorrow").show();
$("#daySelector").html('<a href="#" class="today">this day</a> | next day');
}
if ($(this).hasClass("today")) {
$("#tomorrow").hide();
$("#today").show();
$("#daySelector").html('this day | <a href="#" class="tomorrow">next day</a>');
}
return false;
});
Remove the click handler from the link, add a selector attribute: 从链接中删除点击处理程序,添加选择器属性:
echo '<p id="daySelector">today | <a href="#" selector="tomorrow">tomorrow</a></p>';
Add a click handler: 添加点击处理程序:
$('#daySelector').on('click','a', function() {
return switchDay(this.attr("selector"));
});
It should be noted that live
is deprecated as of 1.7 应当注意,自1.7开始不推荐使用
live
function switchDay(selector) {
if (selector == "tomorrow") {
$("#today").hide();
$("#tomorrow").show();
$("#daySelector").html('<a href="#" selector="today">this day</a> | next day');
} else if (selector == "today") {
$("#tomorrow").hide();
$("#today").show();
$("#daySelector").html('this day | <a href="#" selector="tomorrow">next day</a>');
}
return false;
}
delegate the event to the parent element and you can get rid of the if/else because .toggle() allows you to pass in a condition - true=show/false=hide.. 将事件委托给父元素,您就可以摆脱if / else,因为.toggle()允许您传递条件-true = show / false = hide。
$('#daySelector').on('click','a',function(e){
e.preventDefault(); // prevent default anchor click action
var day = $(this).text(); // get text to check
$("#tomorrow").toggle(day.indexOf('this') > -1); // show if currently this
$("#today").toggle(day.indexOf('next') > -1); // show if currently next
$(this).text(day.indexOf('this') > -1 ? 'next day':'this day'); // toggle text
});
http://jsfiddle.net/pFDsZ/ http://jsfiddle.net/pFDsZ/
I was just guessing some parts as you didn't show all your html. 我只是在猜测一些部分,因为您没有显示所有的html。
This works: 这有效:
Markup: 标记:
<div id='today'>Content A</div>
<div id='tomorrow'>Content B</div>
<p><a href="#" id='switch'>Switch to <span id='switchText'>tomorrow</span></a></p>
Script: 脚本:
$(document).ready(
function () {
showToday();
$('#switch').click(
function () {
var switchText = $('#switchText').text();
if (switchText == 'tomorrow') {
showTomorrow();
} else {
showToday();
}
}
);
}
);
function showToday() {
$('#today').show();
$('#tomorrow').hide();
$('#switchText').text('tomorrow');
}
function showTomorrow() {
$('#today').hide();
$('#tomorrow').show();
$('#switchText').text('today');
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.