最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Changing up and down arrow with jquery - Stack Overflow

programmeradmin2浏览0评论

Why won't this jQuery change my image to a down arrow like I want it to? When a user opens a drop down, the up arrow changes to a down arrow. I'm doing this with an if else because I don't know any easier way, but it's not working. The arrow stays up the entire time, no matter how many times I click. Here is the jQuery. Should be all thats needed. Thanks!

    $("h3#mpcClients").click(function() {
            $("#hidden0").slideToggle(500); 

            var up = true;

            if (up == true) {

            $("td h3").css("background-image", "url(.png)");
            up=false;
          }
            else if(up == false) {
                $("td h3").css("background-image", "url(.png)");
            up=true;
            }
            });

Why won't this jQuery change my image to a down arrow like I want it to? When a user opens a drop down, the up arrow changes to a down arrow. I'm doing this with an if else because I don't know any easier way, but it's not working. The arrow stays up the entire time, no matter how many times I click. Here is the jQuery. Should be all thats needed. Thanks!

    $("h3#mpcClients").click(function() {
            $("#hidden0").slideToggle(500); 

            var up = true;

            if (up == true) {

            $("td h3").css("background-image", "url(http://www.crm-newsletter./client-emails/images/arrowDown.png)");
            up=false;
          }
            else if(up == false) {
                $("td h3").css("background-image", "url(http://www.crm-newsletter./client-emails/images/arrowUp.png)");
            up=true;
            }
            });
Share Improve this question asked Feb 14, 2012 at 18:45 Michael RaderMichael Rader 5,9678 gold badges35 silver badges44 bronze badges 2
  • could we please see the html that goes with this jquery – mcgrailm Commented Feb 14, 2012 at 18:48
  • because you have "var up=true" in your clickhandler for one. – Mike K. Commented Feb 14, 2012 at 18:49
Add a ment  | 

4 Answers 4

Reset to default 8

See .toggleClass() and set up some css like so:

.up {
    background-image: url("http://www.crm-newsletter./client-emails/images/arrowDown.png")
}
.down {
    background-image: url("http://www.crm-newsletter./client-emails/images/arrowDown.png")
}

Make sure you set up the html to a default arrow direction like so:

<td>
    <h3 class="up">Some text</h3>
</td>

Now for the js to change the css classes of the element:

$("h3#mpcClients").click(function() {
        $("#hidden0").slideToggle(500);

        // This adds a class if the element doesn't already have it
        // if the element already has the class it will remove it.
        $("td h3").toggleClass("up down"); 
});

Everytime the function is called it will set a var up to true. It will go into this clause everytime

    var up = true;

    if (up == true) {

    $("td h3").css("background-image", "url(http://www.crm-newsletter./client-emails/images/arrowDown.png)");
    up=false;
  }
 $("h3#mpcClients").click(function() {
            $("#hidden0").slideToggle(500); 

            $("td h3").toggleClass('up');
});

In your css, by default add the rules for showing the down arrow and in a rule for .up set the background arrow image accordingly.

td h3 { background-image : url(http://www.crm-newsletter./client-emails/images/arrowDown.png);}

td h3.up { background-image : url(http://www.crm-newsletter./client-emails/images/arrowUp.png);}

Much neater.

<script type="text/javascript">
function slideT(id)
{   

    var n=id.split(" ");
    $('#panel'+n[1]).slideToggle('fastest');

    if(n[2]=='up')
    {
        $('#img'+n[1]).find('img').remove();

        $('#img'+n[1]).find('td').append('<img src="../images/down.png" id="'+n[0]+' '+n[1]+' down" style="float:left" onclick="slideT(this.id)" >');

    }
    else
    {
        $('#img'+n[1]).find('img').remove();
        $('#img'+n[1]).find('td').append('<img src="../images/up.png" id="'+n[0]+' '+n[1]+' up" style="float:left" onclick="slideT(this.id)" >');        
    }    
}
</script>

this answer will open ur table panel wich its HTML is sth like this:

<tr  style="cursor: pointer" id="img">
        <td align="center">&nbsp;<?=$value?><img src="../images/down.png" style="float: left;" id="slide>" onclick="slideT(this.id)"></td>
    </tr>

<table class="list_row_container" style="display: none;" id="panel">
</table>   
发布评论

评论列表(0)

  1. 暂无评论