0

我有一个动态生成的列表。

当我单击一条线时,我想切换背景颜色,比如说-red green yellow。例如,当 id 不是动态时,我已经看到了答案。

<ul data-role="listview" id="my-listview"  >
    <c:forEach var="line" items="${tl.reqs}">
        <li id="lnk"><a href="#" onclick= '
            $(this).toggle(function(){
                $(this).css("background-color","green");},
            function(){
                $(this).css("background-color","red");},
            function(){
                $(this).css("background-color","yellow");}
            );'
            ${line.name}</a>
        </li>
    </c:forEach>
4

1 回答 1

1

在链接中添加一个 id(或类):

<a href="#" id="myLink">Test</a>

jQuery:

$('#myLink').click(function() {
    var color = $(this).css("background-color");
    console.log(color);
    switch(color) {
        case 'rgb(255, 255, 0)': 
            $(this).css("background-color", "green");
            break;
        case 'rgb(0, 128, 0)': 
            $(this).css("background-color", "red");
            break;
        case 'rgb(255, 0, 0)': 
        default:
            $(this).css("background-color", "yellow");
            break;
    }
    return false;
});

http://jsfiddle.net/samliew/SJwLy/9/

于 2013-02-28T03:39:11.190 回答