如果特定的 html 元素包含特定值,如何向它们添加类

发布于 2024-12-19 21:01:13 字数 755 浏览 1 评论 0原文

例如,我有以下 html:

<div id="stuff">
<span>1</span><span>2</span><span>3</span>
</div>

我需要以这种方式选择一些:

$('#stuff').find('<span>1</span><span>2</span>').addClass('2');

这应该将 html 变成这样:

   <div id="stuff">
    <span class="2">1</span><span class="2">2</span><span>3</span>
    </div>

但是,上面的方法不起作用。有人知道如何使用 jQuery 通过查找 html 直接从 DOM 中选择内容吗?

另外,跨度必须按顺序选择,所以如果我有这个:

<div id="stuff">
<span>2</span><span>3</span><span>1</span>
</div>

并使用上面的JS,它不会做任何事情。

For example, I have the below html:

<div id="stuff">
<span>1</span><span>2</span><span>3</span>
</div>

And I need to select some in this fashion:

$('#stuff').find('<span>1</span><span>2</span>').addClass('2');

Which should turn the html into this:

   <div id="stuff">
    <span class="2">1</span><span class="2">2</span><span>3</span>
    </div>

However, the above does not work. Anyone know how to use jQuery to select things directly off the DOM by looking for the html?

Also, the span's must be selected in order, so if I had this:

<div id="stuff">
<span>2</span><span>3</span><span>1</span>
</div>

And used the above JS, it would not do anything.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

尴尬癌患者 2024-12-26 21:01:13

使用 $( '#stuff span:not(:contains(3))' ).addClass( '2' )

Use $( '#stuff span:not(:contains(3))' ).addClass( '2' )

何处潇湘 2024-12-26 21:01:13

对了,如果你想查找各种内容,请执行以下操作:

<html>
   <head>
          <script type="text/javascript" src="http://code.jquery.com/jquery.js"></script>
          <script type="text/javascript">
          $(function(){
                 var contains = [ 1 , 2 , 6 , 7 , 8 ] ;
                 $.map ( contains, function ( i ) {
                        $ ( '#stuff span:contains("'+ i +'")' ).addClass ( '2' ) ;
                 } ) ;
          });
          </script>
   </head>
   <body>
          <div id="stuff">
                 <span>1</span>
                 <span>2</span>
                 <span>3</span>
                 <span>4</span>
                 <span>5</span>
                 <span>6</span>
                 <span>7</span>
                 <span>8</span>
          </div>
   </body>

输出:

<div id="stuff">
       <span class="2">1</span>
       <span class="2">2</span>
       <span>3</span>
       <span>4</span>
       <span>5</span>
       <span class="2">6</span>
       <span class="2">7</span>
       <span class="2">8</span>
</div>

right, if you want to look for various contents, do this:

<html>
   <head>
          <script type="text/javascript" src="http://code.jquery.com/jquery.js"></script>
          <script type="text/javascript">
          $(function(){
                 var contains = [ 1 , 2 , 6 , 7 , 8 ] ;
                 $.map ( contains, function ( i ) {
                        $ ( '#stuff span:contains("'+ i +'")' ).addClass ( '2' ) ;
                 } ) ;
          });
          </script>
   </head>
   <body>
          <div id="stuff">
                 <span>1</span>
                 <span>2</span>
                 <span>3</span>
                 <span>4</span>
                 <span>5</span>
                 <span>6</span>
                 <span>7</span>
                 <span>8</span>
          </div>
   </body>

The output:

<div id="stuff">
       <span class="2">1</span>
       <span class="2">2</span>
       <span>3</span>
       <span>4</span>
       <span>5</span>
       <span class="2">6</span>
       <span class="2">7</span>
       <span class="2">8</span>
</div>
葬花如无物 2024-12-26 21:01:13

您可以使用 :contains 伪类:

$('#stuff span:contains(1), #stuff span:contains(2)').addClass('2');

http://jsfiddle.net/SJxTu/< /a>

You can use the :contains pseudo-class:

$('#stuff span:contains(1), #stuff span:contains(2)').addClass('2');

http://jsfiddle.net/SJxTu/

愛放△進行李 2024-12-26 21:01:13

我不完全清楚你的选择条件是什么,但你可以使用 jquery 过滤方法和函数来选择范围。

例如,来自文档:

$('li').filter(function(index) {
   return $('strong', this).length == 1;
}).css('background-color', 'red');

http://api.jquery.com/filter/

您需要检查 $(this).text() === 是否任意

I'm not totally clear on what your selection condition is, but you Could use the jquery filter method with a function to select the spans.

For example from the docs:

$('li').filter(function(index) {
   return $('strong', this).length == 1;
}).css('background-color', 'red');

http://api.jquery.com/filter/

You would need to check if $(this).text() === whatever

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文