简体   繁体   English

为特定字符前后的文本字符串上色

[英]Color a text string before and after a specific character

I have some data that can only be sorted in specific ways due to the way it's outputted. 我有一些数据只能根据特定的方式排序,这取决于它们的输出方式。

I have a table with such text strings as 我的桌子上有这样的文字字符串

MyTextString_MyTextString
MyTextString_MyTextString
MyTextString_MyTextString

To make it more readable, I'm looking to color the text before _ red and the text after _ blue. 为了使它更具可读性,我希望为_红色之前的文本和_蓝色之后的文本着色。 Is this possible with jquery, or php? 这可能与jquery或php吗?

I have tried things such as $('.myElement').find('_').css('color','red); 我已经尝试过诸如$('.myElement').find('_').css('color','red); but don't think that is the right approach. 但不要认为这是正确的方法。

Is it possible to only do this once, such as if there is another text string it will remain the second color, blue? 是否可以只执行一次,例如,如果还有另一个文本字符串,它将保持第二种颜色蓝色? ie Red_Blue_Blue_Blue (Count = 1?) 即Red_Blue_Blue_Blue(Count = 1?)

Example of markup. 标记示例。

<td class="myTextString1">
    MyTextString_MyTextString1
    <br/>
    MyTextString_MyTextString2
    <br/>
    MyTextString_MyTextString3
</p>

I've done a fiddle to show (in my opinion) a nice way to do this. 我做了一个小提琴,以展示(我认为)一种不错的方法。 Here is the fiddle 这是小提琴

There are two plugins, one that extend String.prototype and another one that extends jQuery.fn. 有两个插件,一个扩展String.prototype,另一个扩展jQuery.fn。 By doing this way you call ".colorize()" both from a string and an element. 通过这种方式,您可以同时从字符串和元素中调用“ .colorize()”。 I've included some examples in the fiddle. 我在提琴中包含了一些示例。

String.prototype.colorize = function () {
    var regex = new RegExp('[^_]+', 'gi'),
        color = $.extend(['blue', 'red'], arguments || []),
        index = 0;    

    return this.replace(regex, function(matched) {
        return '<span style="color: ' + color[index++] + ';">' + matched + '</span>';
    });
}

$.fn.colorize = function () {
    var args = arguments;

    return this.each(function () {
        this.innerHTML = this.innerHTML.colorize
            .apply(this.innerHTML, args);
    });
}

Hope this helps! 希望这可以帮助!

PHP: PHP:

$stringParts = explode( '_', 'MyTextString_MyTextString' );
$stringParts[0] = sprintf( '<span style="color: #f00;">%s</span>', $stringParts[0] );
if( count( $stringParts ) > 1 )
{
    $stringParts[1] = sprintf( '<span style="color: #00f;">%s</span>', $stringParts[1] );
    /* …or whatever */
}
echo implode( '_', $stringParts );

js: JS:

var colorize = function(text) {
  var delimiter = "_",
      parts = text.split(delimiter),
      firstPart = $("<span>", { class: 'red', html: parts[0] }),
      secondPart = $("<span>", { class: 'blue', html: text.split(parts[0] + delimiter)[1] });

  return $("<div>").append(firstPart).append(delimiter).append(secondPart).html();
}

edit: You could also utilize a helper function that takes jQuery elements like this: 编辑:您也可以利用一个辅助函数,该函数需要jQuery元素,如下所示:

var colorizeElements = function($elements) {
  // $elements has to be something like $("td.classname")
  $elements.each(function() {
    $(this).html(colorize($(this).html()));
  });
};

edit: working jsFiddle: http://jsfiddle.net/tGV54/1/ 编辑:工作jsFiddle: http : //jsfiddle.net/tGV54/1/

edit: based on Kristoffers code: 编辑:基于Kristoffers代码:

String.prototype.colorize = function () {
    var regex = new RegExp('[^_]+', 'gi'),
        color = $.extend(['blue', 'red'], arguments || []),
        index = 0;

    return this.replace(regex, function(matched) {
        return '<span style="color: ' + color[index++] + ';">' + matched + '</span>';
    });
};

$.fn.customColorize = function () {
    var args = arguments,
        delimiter = "<br>";

    return this.each(function () {
        var splitetElements = $.map(this.innerHTML.split(delimiter), function(s) { return s.colorize(); });
        this.innerHTML = splitetElements.join(delimiter);
    });
};

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

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