简体   繁体   English

如何根据列条件设置默认颜色

[英]How to set default color based on a column condition

<table cellspacing="2" cellpadding="2" align="Center" border="0" id="ctl00_centerContent_GridView1" style="border-color:Gray;border-width:1px;border-style:solid;">
    <tbody>
        <tr style="background-color:SeaShell;">
            <td class="nonpar" style="color:Red;font-weight:bold;"><a href="javascript:void(0);" style="color: black; cursor: default;">Entry #1</a></td>
            <td align="center" style="color:Red;">&nbsp;</td>
            <td align="center">N</td>
            <td>&nbsp;</td>
        </tr>
        <tr style="background-color:SeaShell;">
            <td class="nonpar" style="color:Red;font-weight:bold;"><a href="javascript:void(0);" style="color: black; cursor: default;">Entry #2</a></td>
            <td align="center" style="color:Red;">&nbsp;</td>
            <td align="center">N</td>
            <td>&nbsp;</td>
        </tr>
        <tr style="background-color:SeaShell;">
            <td class="nonpar" style="color:Red;font-weight:bold;"><a href="javascript:void(0);" style="color: black; cursor: default;">Entry #3</a></td>
            <td align="center" style="color:Red;">&nbsp;</td>
            <td align="center">Y</td>
            <td>&nbsp;</td>
        </tr>
        <tr style="background-color:SeaShell;">
            <td class="nonpar" style="color:Red;font-weight:bold;"><a href="javascript:void(0);" style="color: black; cursor: default;">Entry #4</a></td>
            <td align="center" style="color:Red;">&nbsp;</td>
            <td align="center">Y</td>
            <td>&nbsp;</td>
        </tr>
    </tbody>
</table>

I am setting the Url, the color, and the cursor through a JQuery: 我通过JQuery设置Url,颜色和光标:

$("#ctl00_centerContent_GridView1 tr td a").each(function () {
    var url = $(this).attr("href");
    var cid = getParameterByName(url, 'ccid');
    if (cid == "34" || cid == "37") {
        $(this)
            .attr('href', 'javascript:void(0);')
            .css({ color: 'black', cursor: 'default' });
    }
});

How can I modify the code so if the query string is 34 or 37 and if the third column is a Y make the font color red instead of black otherwise make the font color red. 如何修改代码,以便查询字符串为3437 ,如果第三列为Y,则使字体颜色为红色而不是黑色,否则将字体颜色设置为红色。 Leave everything else in the code as is. 保留代码中的所有其他内容。

You can find the third column like this:- 你可以找到这样的第三列: -

 var url = $(this).attr("href");
 var cid = getParameterByName(url, 'ccid');
 var parentrow = $(this).parents('tr')[0]; //find the parent tr
 var thirdColumn = $('td:nth-child(3)', parentrow); //find 3rd td in that tr

Then, simply apply the condition:- 然后,只需应用条件: -

if (cid == "34" || cid == "37") {
     if (thirdColumn.text() == "Y")
          $(this).css({ color: 'red', cursor: 'default' });
     else
          $(this).attr('href', 'javascript:void(0);')
                 .css({ color: 'black', cursor: 'default' });
}

You can put a class on the cell when it's a Y, and then find by that class as specified in this answer . 当单元格为Y时,您可以在单元格上放置一个类,然后按照此答案中的指定查找该类。

Just add something like this in your codebehind for the row (pseudo-code): 只需在你的代码隐藏中添加这样的东西(伪代码):

if (cell.Value == "Y")
{
cell.CssClass="something"
}

Then you can pick up the class with jQuery's $(".something") syntax. 然后你可以用jQuery的$(".something")语法来学习这个类。

编辑(所以当我输入这个时,上面的回答者已经提出了这个问题)如果我正确地理解了这一点,你可以简单地在<td>添加一个类,其内容为'Y',而不是以$(this)为目标$(this)使用$(this.new_class)或怎么过你选择),并应用更改。

You need to differentiate Y/N td from other td elements. 您需要将Y/N td与其他td元素区分开来。 In this example I have used .YN class to distinguish 在这个例子中,我使用.YN类来区分

 var getParameterByName = function(url, name) { if (!url) url = location.href; name = name.replace(/[\\[]/, "\\\\\\[").replace(/[\\]]/, "\\\\\\]"); var regexS = "[\\\\?&]" + name + "=([^&#]*)"; var regex = new RegExp(regexS); var results = regex.exec(url); return results == null ? null : results[1]; }; $("#ctl00_centerContent_GridView1 tr td a").each(function() { var url = $(this).attr("href"); var cid = getParameterByName(url, 'ccid'); if ((cid == "34" || cid == "37") && $(this).parents('tr').find('.YN').text() === 'Y') { $(this) .attr('href', 'javascript:void(0);') .css({ color: 'red', cursor: 'default' }); } }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> <table cellspacing="2" cellpadding="2" align="Center" border="0" id="ctl00_centerContent_GridView1" style="border-color:Gray;border-width:1px;border-style:solid;"> <tbody> <tr style="background-color:SeaShell;"> <td class="nonpar" style="color:Red;font-weight:bold;"><a href="javascript:void(0);" style="color: black; cursor: default;">Entry #1</a> </td> <td align="center" style="color:Red;">&nbsp;</td> <td align="center" class="YN">N</td> <td>&nbsp;</td> </tr> <tr style="background-color:SeaShell;"> <td class="nonpar" style="color:Red;font-weight:bold;"><a href="javascript:void(0);" style="color: black; cursor: default;">Entry #2</a> </td> <td align="center" style="color:Red;">&nbsp;</td> <td align="center" class="YN">N</td> <td>&nbsp;</td> </tr> <tr style="background-color:SeaShell;"> <td class="nonpar" style="color:Red;font-weight:bold;"><a href="javascript:void(0);" style="color: black; cursor: default;">Entry #3</a> </td> <td align="center" style="color:Red;">&nbsp;</td> <td align="center" class="YN">Y</td> <td>&nbsp;</td> </tr> <tr style="background-color:SeaShell;"> <td class="nonpar" style="color:Red;font-weight:bold;"><a href="http://localhost/SO/index.html?ccid=37" style="color: black; cursor: default;">Entry #4</a> </td> <td align="center" style="color:Red;">&nbsp;</td> <td align="center" class="YN">Y</td> <td>&nbsp;</td> </tr> </tbody> </table> 

1. Add a class to each <td> who has the Y/N value like this: 1.为每个具有Y / N值的<td>添加一个类,如下所示:

 <td align="center" class="yes_no">Y</td>

2. Update your each to look at sibling .yes_no text like this: 2.更新您的each以查看兄弟.yes_no文本,如下所示:

$("#ctl00_centerContent_GridView1 tr td a").each(function () {
    var url = $(this).attr("href");
    var cid = getParameterByName(url, 'ccid');
    var yes_no_value = $(this).parent('td').siblings('.yes_no').text();
    if (cid == "34" || cid == "37") {
        var new_color = (yes_no_value == 'N') ? 'red' : 'black';
        $(this)
            .attr('href', 'javascript:void(0);')
            .css({ color: new_color, cursor: 'default' });
    }
});

I'd rather see this done with css (not writing styles directly as html attributes), but here you go: 我宁愿用css看到这个(不直接把样式写成html属性),但是你走了:

$('#ctl00_centerContent_GridView1 tr td a').each(function () {
    var url = $(this).attr('href'),
        cid = getParameterByName(url, 'ccid');

    if (cid == '34' || cid == '37') {
        var $parent = $(this).parents('tr');

        if ($parent.children('td:nth-child(3)').text() === 'Y') {
            var $children = $parent.children('td');

            $children
                .css({ 'color': 'red' })
                .find('a').css({ 'color': 'red' });
        }
    }
});

Feel free to condense the code. 随意浓缩代码。 I've assigned variables so you can easily tell what's happening. 我已经分配了变量,因此您可以轻松地分辨出正在发生的事情。

 'use strict'; var cid = '34'; $(document).on('ready', function() { $('#ctl00_centerContent_GridView1 tr td a').each(function() { var url = $(this).attr('href'); //, //cid = getParameterByName(url, 'ccid'); if (cid == '34' || cid == '37') { var $parent = $(this).parents('tr'); if ($parent.children('td:nth-child(3)').text() === 'Y') { var $children = $parent.children('td'); $children.css({ 'color': 'red' }) .find('a').css({ 'color': 'red' }); } } }); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <table cellspacing="2" cellpadding="2" align="Center" border="0" id="ctl00_centerContent_GridView1" style="border-color:Gray;border-width:1px;border-style:solid;"> <tbody> <tr style="background-color:SeaShell;"> <td class="nonpar" style="color:Red;font-weight:bold;"><a href="javascript:void(0);" style="color: black; cursor: default;">Entry #1</a> </td> <td align="center" style="color:Red;">&nbsp;</td> <td align="center">N</td> <td>&nbsp;</td> </tr> <tr style="background-color:SeaShell;"> <td class="nonpar" style="color:Red;font-weight:bold;"><a href="javascript:void(0);" style="color: black; cursor: default;">Entry #2</a> </td> <td align="center" style="color:Red;">&nbsp;</td> <td align="center">N</td> <td>&nbsp;</td> </tr> <tr style="background-color:SeaShell;"> <td class="nonpar" style="color:Red;font-weight:bold;"><a href="javascript:void(0);" style="color: black; cursor: default;">Entry #3</a> </td> <td align="center" style="color:Red;">&nbsp;</td> <td align="center">Y</td> <td>&nbsp;</td> </tr> <tr style="background-color:SeaShell;"> <td class="nonpar" style="color:Red;font-weight:bold;"><a href="javascript:void(0);" style="color: black; cursor: default;">Entry #4</a> </td> <td align="center" style="color:Red;">&nbsp;</td> <td align="center">Y</td> <td>&nbsp;</td> </tr> </tbody> </table> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM