Javascript 在单击时切换div中的字符
当单击Javascript 在单击时切换div中的字符,javascript,jquery,html,Javascript,Jquery,Html,当单击.articletTitle时,我希望下面的代码能够在向上和向下箭头之间切换.articleArrow的html内容: jQuery $(".articleTitle").click(function () { if ($(this).children('.articleArrow').html() == '↑') $(this).children('.articleArrow').html('↓'); else if
.articletTitle
时,我希望下面的代码能够在向上和向下箭头之间切换.articleArrow
的html内容:
jQuery
$(".articleTitle").click(function () {
if ($(this).children('.articleArrow').html() == '↑')
$(this).children('.articleArrow').html('↓');
else if ($(this).children('.articleArrow').html() == '↓')
$(this).children('.articleArrow').html('↑');
});
HTML
<div class='articleTitle'>
Blah
<div class='articleArrow'>
↓
</div>
</div>
废话
↓
但它什么也没用。另一方面,如果我取出if…else if
并将字符设置为$(this).children('.articleArrow').html('↑;')代码>它可以工作。因此,设置角色是有效的,如果没有正确触发,则是if…else
,我不知道为什么
(不要激动,这不是实际的管理面板!)如果我使用unicode字符来比较:
$(".articleTitle").click(function () {
if ($(this).children('.articleArrow').html() == '↓') $(this).children('.articleArrow').html('↑');
else if ($(this).children('.articleArrow').html() == '↑') $(this).children('.articleArrow').html('↓');
});
如果我使用unicode字符来比较:
$(".articleTitle").click(function () {
if ($(this).children('.articleArrow').html() == '↓') $(this).children('.articleArrow').html('↑');
else if ($(this).children('.articleArrow').html() == '↑') $(this).children('.articleArrow').html('↓');
});
如果我使用unicode字符来比较:
$(".articleTitle").click(function () {
if ($(this).children('.articleArrow').html() == '↓') $(this).children('.articleArrow').html('↑');
else if ($(this).children('.articleArrow').html() == '↑') $(this).children('.articleArrow').html('↓');
});
如果我使用unicode字符来比较:
$(".articleTitle").click(function () {
if ($(this).children('.articleArrow').html() == '↓') $(this).children('.articleArrow').html('↑');
else if ($(this).children('.articleArrow').html() == '↑') $(this).children('.articleArrow').html('↓');
});
只需使用's和jQuery的选择器,您就可以轻松地完成此操作,而无需使用凌乱的if语句
$(".articleTitle").click(function () {
// 1st, i go ahead and asign our arrow element to a variable for use in callback
var arrow = $(this).find('.articleArrow');
$(this).siblings('.articleContent').slideToggle("slow", function(){
// this is the `complete` callback method
// in here, we can now manipulate whatever we need to when the "toggle" is `complete`
arrow.html( $(this).is(':visible') ? '↑' : '↓' );
// inside the .html statement is a simple `inline if` statement that simply says:
// if true ? do this : else do this
});
});
普通代码:
$(".articleTitle").click(function () {
var a = $(this).find(".articleArrow");
$(this).siblings(".articleContent").slideToggle("slow", function () {
a.html($(this).is(":visible") ? "↑" : "↓")
})
});
只需使用's和jQuery的选择器,就可以轻松地完成此操作,而无需使用凌乱的if语句
$(".articleTitle").click(function () {
// 1st, i go ahead and asign our arrow element to a variable for use in callback
var arrow = $(this).find('.articleArrow');
$(this).siblings('.articleContent').slideToggle("slow", function(){
// this is the `complete` callback method
// in here, we can now manipulate whatever we need to when the "toggle" is `complete`
arrow.html( $(this).is(':visible') ? '↑' : '↓' );
// inside the .html statement is a simple `inline if` statement that simply says:
// if true ? do this : else do this
});
});
普通代码:
$(".articleTitle").click(function () {
var a = $(this).find(".articleArrow");
$(this).siblings(".articleContent").slideToggle("slow", function () {
a.html($(this).is(":visible") ? "↑" : "↓")
})
});
只需使用's和jQuery的选择器,就可以轻松地完成此操作,而无需使用凌乱的if语句
$(".articleTitle").click(function () {
// 1st, i go ahead and asign our arrow element to a variable for use in callback
var arrow = $(this).find('.articleArrow');
$(this).siblings('.articleContent').slideToggle("slow", function(){
// this is the `complete` callback method
// in here, we can now manipulate whatever we need to when the "toggle" is `complete`
arrow.html( $(this).is(':visible') ? '↑' : '↓' );
// inside the .html statement is a simple `inline if` statement that simply says:
// if true ? do this : else do this
});
});
普通代码:
$(".articleTitle").click(function () {
var a = $(this).find(".articleArrow");
$(this).siblings(".articleContent").slideToggle("slow", function () {
a.html($(this).is(":visible") ? "↑" : "↓")
})
});
只需使用's和jQuery的选择器,就可以轻松地完成此操作,而无需使用凌乱的if语句
$(".articleTitle").click(function () {
// 1st, i go ahead and asign our arrow element to a variable for use in callback
var arrow = $(this).find('.articleArrow');
$(this).siblings('.articleContent').slideToggle("slow", function(){
// this is the `complete` callback method
// in here, we can now manipulate whatever we need to when the "toggle" is `complete`
arrow.html( $(this).is(':visible') ? '↑' : '↓' );
// inside the .html statement is a simple `inline if` statement that simply says:
// if true ? do this : else do this
});
});
普通代码:
$(".articleTitle").click(function () {
var a = $(this).find(".articleArrow");
$(this).siblings(".articleContent").slideToggle("slow", function () {
a.html($(this).is(":visible") ? "↑" : "↓")
})
});
你想让箭头字符在按下时切换吗?(向上箭头和向下箭头)@Austin是的,我希望它在单击父div时在向上和向下之间切换。一旦HTML实体插入DOM中,实体将被解析,不再是实体,因此HTML()
不会返回实体,你可以通过将你的值记录到控制台来计算出来。那么你想让箭头字符在按下时切换吗?(向上箭头和向下箭头)@Austin是的,我希望它在单击父div时在向上和向下之间切换。一旦HTML实体插入DOM中,实体将被解析,不再是实体,因此HTML()
不会返回实体,你可以通过将你的值记录到控制台来计算出来。那么你想让箭头字符在按下时切换吗?(向上箭头和向下箭头)@Austin是的,我希望它在单击父div时在向上和向下之间切换。一旦HTML实体插入DOM中,实体将被解析,不再是实体,因此HTML()
不会返回实体,你可以通过将你的值记录到控制台来计算出来。那么你想让箭头字符在按下时切换吗?(向上箭头和向下箭头)@Austin是的,我希望它在单击父div时在向上和向下之间切换。一旦HTML实体插入DOM中,实体将被解析,不再是实体,因此HTML()
不会返回实体,您可以通过将您的值记录到控制台来了解这一点。感谢您了解了一些新内容!我是jQuery新手,但如果我理解正确,那么对“:visible”的检查只会返回true(当然,如果它是可见的)。但我可以用“真”来代替它,或者其他任何返回真的东西?可见检查在这里没有其他用途不?@有趣的是,可见检查在这里用于决定元素是打开还是关闭。如果它已关闭,则不可见。你可以用任何东西来代替它,但我不确定还有什么东西能满足你在这里想要实现的目标?谢谢,我学到了一些新东西!我是jQuery新手,但如果我理解正确,那么对“:visible”的检查只会返回true(当然,如果它是可见的)。但我可以用“真”来代替它,或者其他任何返回真的东西?可见检查在这里没有其他用途不?@有趣的是,可见检查在这里用于决定元素是打开还是关闭。如果它已关闭,则不可见。你可以用任何东西来代替它,但我不确定还有什么东西能满足你在这里想要实现的目标?谢谢,我学到了一些新东西!我是jQuery新手,但如果我理解正确,那么对“:visible”的检查只会返回true(当然,如果它是可见的)。但我可以用“真”来代替它,或者其他任何返回真的东西?可见检查在这里没有其他用途不?@有趣的是,可见检查在这里用于决定元素是打开还是关闭。如果它已关闭,则不可见。你可以用任何东西来代替它,但我不确定还有什么东西能满足你在这里想要实现的目标?谢谢,我学到了一些新东西!我是jQuery新手,但如果我理解正确,那么对“:visible”的检查只会返回true(当然,如果它是可见的)。但我可以用“真”来代替它,或者其他任何返回真的东西?可见检查在这里没有其他用途不?@有趣的是,可见检查在这里用于决定元素是打开还是关闭。如果它已关闭,则不可见。你可以用任何东西来代替它,但我不确定还有什么东西能满足你在这里想要实现的目标?