代码之家  ›  专栏  ›  技术社区  ›  markratledge

使用jQuery在文档加载时更改<span>文本

  •  2
  • markratledge  · 技术社区  · 14 年前

    <span> 在该菜单中,jQuery打开document ready。跨度嵌套为 <li> 我不知道这有什么不同(我也不认为WordPress有什么不同。)还有其他的 <span>'s <跨度> 我想改变。

    我该怎么改变这个 <span>We are here to help.</span>

    为了这个

    <span style="color:#123456;">We are here to help.</span>

    $(document).ready ?

    完整的html是

    <ul id="nav"><li class="li_item"><a class="navlink" href="http://www.mysite.org/contact-us/"><strong>Contact Us</strong><span>We are here to help.</span></a></li></ul>

    更新:

    jQuery(function($) {
    $(document).ready(function() {
    $("#nav li span:contains('We are here to help.')")
    .css("color", "#f8481c"); 
    });
    });
    
    2 回复  |  直到 14 年前
        1
  •  5
  •   Matt Mitchell    14 年前

    试试这个(假设) nav 是的id ul

    $(document).ready(function() {
        $("#nav li span:contains('Text text text')")
          .css("color", "#123456"); 
    });
    

    以你为例 this jsFiddle 作品:

        2
  •  1
  •   Mitch Rosenburg    14 年前

    如果:contains对您不起作用,则应该可以这样做。

    $(function() {
      $('#nav li span').each(function() {
        if ($(this).text() === 'We are here to help.')
          $(this).css('color', '#123456');
      });
    });