【问题标题】:Jquery Each function, if statement doesn't workJquery 每个函数,if 语句不起作用
【发布时间】:2011-10-10 09:30:47
【问题描述】:
<head>
    <title>Untitled Page</title>
    <script src="jquery-1.4.1.js"></script>
    <script type="text/jscript">
        function changecolor() 
        {


            $('li').each(function() 
            {
                var sval = $(this).text();
                //alert(sval);
                if (sval == "Rose") 
                {
                    alert(sval);
                    $(item).css('backgroundcolor', 'red');
                }

            });
        }
    </script>
</head>
<body>
    Select your fraglance : 
    <ul>
        <li>Rose</li>
        <li>White Jasmine</li>
        <li>Yellow Sunflower</li>
        <li>Pink Lotus</li>
        <li>White Daisy</li>
    </ul>
    <button id="text"  onclick="changecolor()">Change Color</button>
</body>
</html>

【问题讨论】:

    标签: jquery if-statement each


    【解决方案1】:

    item 未在某处定义。

    看这个例子: http://jsfiddle.net/pepMJ/

    【讨论】:

      【解决方案2】:

      您可能想要使用 background-color,因为那是 CSS 名称。

      $(item).css('background-color', 'red');
      

      【讨论】:

        【解决方案3】:

        js中的item在哪里定义?可能是它的一个错误。试试这个

        <script type="text/jscript">
                function changecolor() 
                {
        
        
                    $('li').each(function() 
                    {
                        var sval = $(this).text();
                        //alert(sval);
                        if (sval == "Rose") 
                        {
                            alert(sval);
                            $(this).css('backgroundcolor', 'red');
                        }
        
                    });
                }
            </script>
        

        【讨论】:

          【解决方案4】:

          我认为您忘记了 .each 中的项目

          $('li').each(function(i, item)  
          

          【讨论】:

            猜你喜欢
            • 2017-10-22
            • 1970-01-01
            • 2014-09-15
            • 2013-03-05
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2014-03-21
            相关资源
            最近更新 更多