更改选择字段的颜色也会更改与该选择字段

时间:2015-04-27 12:49:45

标签: javascript css

如果color: red;遇到错误的状态,我会在var gfields = e.target.parentNode.parentNode.parentNode.getElementsByClassName('gfield'); for(var i = 0; i < gfields.length; i++) { var field = gfields[i].getElementsByTagName('select'); if(field.length) { if(field[0].id != e.target.id) { var target_value = e.target.value; if(field[0].value == e.target.value) { e.target.style.color = "Red"; //here break; } else { e.target.style.color = "rgb(68, 68, 68)"; //and here } } } } 设置无效的选择/选项字段,如下所示:

def detectPattern( pattern, line, universal=None):
    """ Return the index where a pattern starts in the line. """    
    index = 0

    indexes = []

    while ( (index + len(pattern)) < len(line) ):

        match = True

        for patternIndex in range(len(pattern)):
            exactMatching = (line[index + patternIndex] == pattern[patternIndex])
            universalMatching = (line[index + patternIndex] == universal )
            if not (exactMatching or universalMatching) :
                match = False

        if match :
            indexes.append(index)

        index += 1

    return indexes

def prices(prices, line, cumulative = False, universal = '1'):
    result = 0
    patterns = []

    for price in prices:
        patternIndexes = detectPattern(price, line, universal = universal)

        if patternIndexes and not cumulative:
            result += prices[price]
        elif patternIndexes and cumulative:
            result += prices[price] * len(patternIndexes)

        patterns.append((price, patternIndexes))

    return result, patterns

print(prices( {"foo":1, "fooo":10} , "foobarffoofoobar"))
print(prices( {"foo":1, "fooo":10} , "foobarffoofoobar", cumulative = True))
print(prices( {"foo":1, "fooo":10} , "f1obarf1oofoobar"))
print(prices( {"foo":1, "fooo":10} , "f1obarf1oofoobar", cumulative = True))

工作正常但是 - 这样做也可以将选择框中所有选项的颜色设置为红色,任何想法是否有设置它的方法,只显示所显示的颜色在选择框中,而不是该框的子选项被更改?

提前干杯。

2 个答案:

答案 0 :(得分:0)

如果我做了

e.target.style.color = "Red";
var options = e.target.childNodes;
for(var x = 0; x < options.length; x++) {
    options[x].style.color = "rgb(68, 68, 68)";
}

它解决了我的问题,特别设计了每个单独的选项,以获得默认颜色,位&#39; hacky&#39;但我觉得有效。

答案 1 :(得分:0)

您也可以尝试仅从选择框中为selectedIndex值着色:

var url = '/feed/location';
callGetAjax(url,function(result)
{
    //console.log(result); <= of course this logs right
    data = result;
    return data;
});
console.log(result); // <= ReferenceError: result is not defined
console.log(data); // <= ReferenceError: data is not defined