Currently I have a heading with a downwards pointing icon. If the header is clicked, I want the image to change to an upwards one.
目前我有一个带有向下指向图标的标题。如果单击标题,我希望图像更改为向上图像。
I have tried using the "?" operator to query if it is, but I am not 100% sure how it works. I'm using this code at the moment.
我试过用“?”运算符查询是否,但我不是100%确定它是如何工作的。我现在正在使用此代码。
// Toggle message_body
$(".message_head").click(function(){
var id = $(this).attr("id");
$(this).next(".message_body").slideToggle(500);
$(".icon[id=" + id + "]").attr("src", "../images/admin/Symbol_Down.png" ? : "../images/admin/Symbol_Up.png");
return false;
});
I know this code does not work. Could someone please show me how? Thanks!
我知道这段代码不起作用。有人可以告诉我怎么样?谢谢!
3 个解决方案
#1
This will do the trick. Note that you need to do the comparison within the terenary operator. If you just do "something" ? "blah" : "result
, it will always be true, because non-null values are always true.
这样就可以了。请注意,您需要在terenary运算符中进行比较。如果你只做“某事”? “blah”:“结果,它将永远是真的,因为非空值总是正确的。
var up = "../images/admin/Symbol_Up.png";
var down = "../images/admin/Symbol_Down.png";
$(".message_head").click(function(){
var icon = $(".icon[id=" + $(this).attr("id") + "]");
$(this).next(".message_body").slideToggle(500);
icon.attr("src", this.attr("src") == up ? down : up);
});
I just realized one fundamental issue with your HTML if your JavaScript css selector is accurate. You are asking for .icon[id=x] and using the ID attribute from the .message_head class to find the ID of the icon. For this to work, you would have to have the same ID for both, which is invalid HTML. What I imagine your HTML looks like is this:
如果您的JavaScript css选择器准确无误,我刚刚意识到HTML的一个基本问题。您要求.icon [id = x]并使用.message_head类中的ID属性来查找图标的ID。为此,您必须为两者都使用相同的ID,这是无效的HTML。我想象你的HTML看起来是这样的:
<div class="message_head" id="1">
<img class="icon" src="up.jpg" id="1"/>
</div>
This is a nono. What you can do is this:
这是一个nono。你能做的是:
<div class="message_head" id="1">
<img class="icon" src="up.jpg" />
</div>
var up = "../images/admin/Symbol_Up.png";
var down = "../images/admin/Symbol_Down.png";
$(".message_head").click(function(){
var icon = $('.icon', this);
$(this).next(".message_body").slideToggle(500);
icon.attr("src", this.attr("src") == up ? down : up);
});
#2
You're not using anything in the conditional.
你没有在条件中使用任何东西。
$(".message_head").click(function(){
var id = $(this).attr("id");
var myBody = $(this).next(".message_body").slideToggle(500);
$(".icon[id=" + id + "]").attr("src", (myBody.css("display") == "none") ? "../images/admin/Symbol_Down.png" : "../images/admin/Symbol_Up.png");
return false;
});
Note that if this produces backwards behavior (i.e., up instead of down), you should change "none"
to "block"
.
请注意,如果这会产生向后行为(即向上而不是向下),则应将“none”更改为“block”。
#3
$(".message_head").click(function(){
this.next(".message_body").slideToggle(500);
this.attr("src", this.attr('src') == "../images/admin/Symbol_Down.png" ? "../images/admin/Symbol_Up.png" : "../images/admin/Symbol_Down.png");
return false;
});
More info on the ?: (ternary operator). This:
有关?:(三元运算符)的更多信息。这个:
var A = B ? C : D;
Is the exact same as:
完全相同:
if (B) {
var A = C
} else {
var A = D;
}
#1
This will do the trick. Note that you need to do the comparison within the terenary operator. If you just do "something" ? "blah" : "result
, it will always be true, because non-null values are always true.
这样就可以了。请注意,您需要在terenary运算符中进行比较。如果你只做“某事”? “blah”:“结果,它将永远是真的,因为非空值总是正确的。
var up = "../images/admin/Symbol_Up.png";
var down = "../images/admin/Symbol_Down.png";
$(".message_head").click(function(){
var icon = $(".icon[id=" + $(this).attr("id") + "]");
$(this).next(".message_body").slideToggle(500);
icon.attr("src", this.attr("src") == up ? down : up);
});
I just realized one fundamental issue with your HTML if your JavaScript css selector is accurate. You are asking for .icon[id=x] and using the ID attribute from the .message_head class to find the ID of the icon. For this to work, you would have to have the same ID for both, which is invalid HTML. What I imagine your HTML looks like is this:
如果您的JavaScript css选择器准确无误,我刚刚意识到HTML的一个基本问题。您要求.icon [id = x]并使用.message_head类中的ID属性来查找图标的ID。为此,您必须为两者都使用相同的ID,这是无效的HTML。我想象你的HTML看起来是这样的:
<div class="message_head" id="1">
<img class="icon" src="up.jpg" id="1"/>
</div>
This is a nono. What you can do is this:
这是一个nono。你能做的是:
<div class="message_head" id="1">
<img class="icon" src="up.jpg" />
</div>
var up = "../images/admin/Symbol_Up.png";
var down = "../images/admin/Symbol_Down.png";
$(".message_head").click(function(){
var icon = $('.icon', this);
$(this).next(".message_body").slideToggle(500);
icon.attr("src", this.attr("src") == up ? down : up);
});
#2
You're not using anything in the conditional.
你没有在条件中使用任何东西。
$(".message_head").click(function(){
var id = $(this).attr("id");
var myBody = $(this).next(".message_body").slideToggle(500);
$(".icon[id=" + id + "]").attr("src", (myBody.css("display") == "none") ? "../images/admin/Symbol_Down.png" : "../images/admin/Symbol_Up.png");
return false;
});
Note that if this produces backwards behavior (i.e., up instead of down), you should change "none"
to "block"
.
请注意,如果这会产生向后行为(即向上而不是向下),则应将“none”更改为“block”。
#3
$(".message_head").click(function(){
this.next(".message_body").slideToggle(500);
this.attr("src", this.attr('src') == "../images/admin/Symbol_Down.png" ? "../images/admin/Symbol_Up.png" : "../images/admin/Symbol_Down.png");
return false;
});
More info on the ?: (ternary operator). This:
有关?:(三元运算符)的更多信息。这个:
var A = B ? C : D;
Is the exact same as:
完全相同:
if (B) {
var A = C
} else {
var A = D;
}