如何根据文本添加其他属性 [英] How to add additional attribute according to the text

查看:77
本文介绍了如何根据文本添加其他属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的HTML代码

<div id="ccc_new_val_hdn">
    <span>9</span>
    <span>,</span>
    <span> </span>
    <span>6</span>
    <span>2</span>
    <span>1</span>
</div>

我需要这样转换逗号和空格

I need to convert comma and space like this

<span class='comma'>,</span>

和空格

<span class='space'> </span>

我尝试了:contains()和find(),但我无法获取代码.

I tried :contains() and find(), but i cannot get the code.

推荐答案

只需迭代并设置类.

$('#ccc_new_val_hdn span').each(function() {
    if($(this).html() == ','){
        $(this).addClass('comma');
    }
    if($(this).html() == ' '){
        $(this).addClass('space');
    }
});

演示: http://jsbin.com/igaraq

这篇关于如何根据文本添加其他属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆