我想基于循环遍历的条件使用jQuery更改字体颜色,以JSON数组为基础。

huangapple go评论140阅读模式
英文:

I want to change font color with jquery based on condition after looping through json array

问题

我正在使用jQuery的.each()循环遍历一个JSON数组,如果值包含单词"CALL",我想将字体颜色更改为红色。我已经尝试了几个示例,但没有成功。

$(function () {
    var FetchMonUrl = '@Url.Action("FetchToday")';
    $.getJSON(FetchMonUrl, { MonID: '@FetchDay' }, function (dataFetch) {
        $.each(dataFetch, function (key, value) {
            $('#TodayName').html(dataFetch);
            alert(this);
            if ($(this).is(':contains("CALL")')) {
                $(this).css('color', 'red');
            } 
        });           
    });
});
英文:

I am looping through a JSON array using .each() in Jquery and I want to change the font color to red if the value contains the word "CALL". I have followed several examples with no luck.

$(function () {
    var FetchMonUrl = '@Url.Action("FetchToday")';
    $.getJSON(FetchMonUrl, { MonID: ('@FetchDay') }, function (dataFetch) {
        $.each(dataFetch, function (key, value) {
            $('#TodayName').html(dataFetch);
            alert(this);
            if ($(this).is(':contains("CALL")')) {
                $(this).css('color', 'red');
            } 
        });           
    });
});

答案1

得分: 0

我怀疑返回的数据是一个数组,因为你正在使用$.each()循环它。如果是这样,请考虑以下内容。

$(function () {
  var FetchMonUrl = '@Url.Action("FetchToday")';
  $.getJSON(FetchMonUrl, { MonID: ('@FetchDay') }, function (dataFetch) {
    $('#TodayName').html(dataFetch.toString());
    $.each(dataFetch, function (key, value) {
      if (value.indexOf("CALL") >= 0) {
        $('#TodayName').css('color', 'red');
      } 
    });           
  });
});

如果返回的数据不是一个数组,而是文本,我建议使用$.get()

:contains是用于元素而不是数据的伪选择器。不能以你尝试使用的方式使用它。.indexOf()会返回一个字符串中特定字符串的索引(或数组中的索引),如果未找到则返回-1

英文:

I suspect thew returning data is an Array, since you're looping it with $.each(). If so, consider the following.

$(function () {
  var FetchMonUrl = '@Url.Action("FetchToday")';
  $.getJSON(FetchMonUrl, { MonID: ('@FetchDay') }, function (dataFetch) {
    $('#TodayName').html(dataFetch.toString());
    $.each(dataFetch, function (key, value) {
      if (value.indexOf("CALL") >= 0) {
        $('#TodayName').css('color', 'red');
      } 
    });           
  });
});

If the data returned is not an Array, but is Text, I would just use $.get().

The :contains is a pseudo selector for elements not data. It can't be used in the manner you tried to use it. .indexOf() will return the Index of the specific string in a string (or Array) or -1 if not found.

答案2

得分: 0

$(function () {
    var FetchMonUrl = '@Url.Action("FetchToday")';
    $.getJSON(FetchMonUrl, { MonID: ('@FetchDay') }, function (dataFetch) {
        $.map(dataFetch, function (value, key) {
            if (value.indexOf("CALL") >= 0) {
                value2 = value.fontcolor('#cd0000');
                dataFetch[key] = value2;
            } 
            $('#TodayName').html(dataFetch);
        });           
    });
});
英文:
$(function () {
    var FetchMonUrl = '@Url.Action("FetchToday")';
    $.getJSON(FetchMonUrl, { MonID: ('@FetchDay') }, function (dataFetch) {
        $.map(dataFetch, function (value, key) {
            if (value.indexOf("CALL") >= 0) {
                value2 = value.fontcolor('#cd0000');
                dataFetch[key] = value2;                    
            } 
            $('#TodayName').html(dataFetch);
            
        });           
    });
});

huangapple
  • 本文由 发表于 2020年1月4日 01:44:41
  • 转载请务必保留本文链接:https://go.coder-hub.com/59583045.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定