如何更改li标签列表的第一项的颜色?

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

How to change the color of the first item of a li tag list?

问题

我想解决这个问题:当点击按钮时,希望每个li标签列表的第一项的颜色发生变化(最好是创建的按钮)。我已经尝试为每个列表中的第一项创建一个id,但没有成功。以下是代码:

$(document).ready(function(){
    $("#botao").click(function(){
        $("p").css("color", "red");
    });
});
<!-- 将以下内容添加到示例中的第二个按钮,文本为“单击此处”,并在单击时将其自己的文本更改为“已单击”。 -->
<!DOCTYPE html>
<html lang="en">
<head>
  <title>jQuery</title>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.2.1/dist/css/bootstrap.min.css" rel="stylesheet">
  <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.2.1/dist/js/bootstrap.bundle.min.js"></script>
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script>
</head>
<body>
<div class="container-fluid p-5 bg-primary text-white text-center">
  <h1>使用jQuery创建页面</h1>
</div>
<div class="container mt-5">
  <div class="row div1">
    <div class="col-sm-4 coluna1">
      <div id="texto">
        <!-- 将以下内容添加到示例中的第二个按钮,文本为“单击此处”,并在单击时将其自己的文本更改为“已单击”。 -->
      </div>
      <div class="col-sm-4 coluna2">
        <p class="classC">Lorem ipsum dolor sit amet, consectetur adipisicing elit...</p>
        <p class="classD">Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris...</p>
      </div>
      <div class="col-sm-4 coluna3">
        <p class="classE">Lorem ipsum dolor sit amet, consectetur adipisicing elit...</p>
        <p class="classF">Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris...</p>
        <p class="classA">Lorem ipsum dolor sit amet, consectetur adipisicing elit...</p>
        <input id="botao" type="button" class="btn btn-warning btn-xs 189738" value="Mudar de cor"/>
        <div>
          <td class="btnDoc">
            <br>
            <button id="dados" data-confirm="" data-iddocumento="189738" data-descricao="testte" type="button" class="btn btn-warning btn-xs 189738">Clique aqui</button>
          </td>
        </div>
        <ul>
          <!-- 创建一个选择器,更改所有列表的第一项文本的颜色。 -->
          <li>item 1</li>
          <li>item 2</li>
          <li>item 3</li>
        </ul>
      </div>
    </div>
    <div>
      <ul>
        <li>Coffee</li>
        <li>Tea</li>
        <li>Milk</li>
      </ul>
    </div>
    <div class="row div3">
      <div>
        <ul>
          <li>C++</li>
          <li>JAVA</li>
          <li>Python</li>
          <li>HTML/CSS</li>
        </ul>
      </div>
    </div>
  </div>
  <div class="row div2">
    <h1>x caracteres</h1>
    <div class="col-12">
      <input class="contar" type="text" value="Vamos contar os caractesres?"/>
    </div>
  </div>
</div>
<div class="container-fluid rodape">
</div>
<script src="2jquery.js"></script>
<script src="Jcollor.js"></script>
</body>
</html>
英文:

I would like to solve this question: I am having problems so that the first item of each li tag list has its color changed when clicking on a button (preferably created). I already tried to create an id for each first item of the lists, inside the li tag, without success. Here is the code below:

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

$(document).ready(function(){
$(&quot;#botao&quot;).click(function(){
$(&quot;p&quot;).css(&quot;color&quot;, &quot;red&quot;);
});
});

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-html -->

&lt;!--- Adicione ao exemplo um segundo bot&#227;o com o texto “Clique aqui” e fa&#231;a com
que ele, ao ser clicado, troque o seu pr&#243;prio texto para “J&#225; clicou”. --&gt;
&lt;!DOCTYPE html&gt;
&lt;html lang=&quot;en&quot;&gt;
&lt;head&gt;
&lt;title&gt;jQuery&lt;/title&gt;
&lt;meta charset=&quot;utf-8&quot;&gt;
&lt;meta name=&quot;viewport&quot; content=&quot;width=device-width, initial-scale=1&quot;&gt;
&lt;link href=&quot;https://cdn.jsdelivr.net/npm/bootstrap@5.2.1/dist/css/bootstrap.min.css&quot; rel=&quot;stylesheet&quot;&gt;
&lt;script src=&quot;https://cdn.jsdelivr.net/npm/bootstrap@5.2.1/dist/js/bootstrap.bundle.min.js&quot;&gt;&lt;/script&gt;
&lt;script src=&quot;https://ajax.googleapis.com/ajax/libs/jquery/3.6.0/jquery.min.js&quot;&gt;&lt;/script&gt;
&lt;/head&gt;
&lt;body&gt;
&lt;div class=&quot;container-fluid p-5 bg-primary text-white text-center&quot;&gt;
&lt;h1&gt;Criando p&#225;ginas com jQuery&lt;/h1&gt; 
&lt;/div&gt;
&lt;div class=&quot;container mt-5&quot;&gt;
&lt;div class=&quot;row div1&quot;&gt;
&lt;div class=&quot;col-sm-4 coluna1&quot;&gt;
&lt;div id=&quot;texto&quot;&gt;
&lt;!-- Adicione ao exemplo um segundo bot&#227;o com o texto “Clique aqui” e fa&#231;a com 
que ele, ao ser clicado, troque o seu pr&#243;prio texto para “J&#225; clicou”. --&gt;   
&lt;/div&gt;
&lt;div class=&quot;col-sm-4 coluna2&quot;&gt;
&lt;p class=&quot;classC&quot;&gt;Lorem ipsum dolor sit amet, consectetur adipisicing elit...&lt;/p&gt;
&lt;p class=&quot;classD&quot;&gt;Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris...&lt;/p&gt;
&lt;/div&gt;
&lt;div class=&quot;col-sm-4 coluna3&quot;&gt;      
&lt;p class=&quot;classE&quot;&gt;Lorem ipsum dolor sit amet, consectetur adipisicing elit...&lt;/p&gt;
&lt;p class=&quot;classF&quot;&gt;Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris...&lt;/p&gt;
&lt;!-- Muda a cor dos par&#225;grafos--&gt;
&lt;p class=&quot;classA&quot;&gt;Lorem ipsum dolor sit amet, consectetur adipisicing elit...&lt;/p&gt;&lt;input id=&quot;botao&quot; type=&quot;button&quot; class=&quot;btn btn-warning btn-xs 189738&quot; value=&quot;Mudar de cor&quot;/&gt;
&lt;!-- Altera o texto do bot&#227;o--&gt;
&lt;div&gt; &lt;td class=&quot;btnDoc&quot;&gt;&lt;br&gt;
&lt;button id=&quot;dados&quot; data-confirm=&quot;&quot; data-iddocumento=&quot;189738&quot; data-descricao=&quot;testte&quot; type=&quot;button&quot; class=&quot;btn btn-warning btn-xs 189738&quot;&gt;Clique aqui&lt;/button&gt;
&lt;/td&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;ul&gt;
&lt;!--Crie um seletor que altere a cor do texto do primeiro item de todas as listas. --&gt;
&lt;li&gt;item 1&lt;/li&gt;
&lt;li&gt;item 2&lt;/li&gt;
&lt;li&gt;item 3&lt;/li&gt;
&lt;/ul&gt;
&lt;/div&gt;
&lt;div&gt;
&lt;ul&gt;
&lt;li&gt;Coffee&lt;/li&gt;
&lt;li&gt;Tea&lt;/li&gt;
&lt;li&gt;Milk&lt;/li&gt;
&lt;/ul&gt;
&lt;/div&gt;
&lt;div class=&quot;row div3&quot;&gt;
&lt;div&gt;
&lt;ul&gt;
&lt;li&gt;C++&lt;/li&gt;
&lt;li&gt;JAVA&lt;/li&gt;
&lt;li&gt;Python&lt;/li&gt;
&lt;li&gt;HTML/CSS&lt;/li&gt;
&lt;/ul&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div class=&quot;row div2&quot;&gt;
&lt;h1&gt;x caracteres&lt;/h1&gt;
&lt;div class=&quot;col-12&quot;&gt;
&lt;input class=&quot;contar&quot; type=&quot;text&quot; value=&quot;Vamos contar os caractesres?&quot;/&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div class=&quot;container-fluid rodape&quot;&gt;
&lt;/div&gt;
&lt;script src=&quot;2jquery.js&quot;&gt;&lt;/script&gt;
&lt;script src=&quot;Jcollor.js&quot;&gt;&lt;/script&gt;
&lt;/body&gt;
&lt;/html&gt;

<!-- end snippet -->

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

&lt;!-- language: lang-js --&gt;
$(&quot;.btn&quot;).click(function(e) {
$(this).text(&quot;J&#225; clicou&quot;);
}); 
&lt;!-- end snippet --&gt;

<!-- end snippet -->

The "click here" button should work with the other js I provided in the question.

答案1

得分: 1

以下是您要翻译的代码部分:

$(document).ready(function(){
    $("#botao").click(function(){
        $("ul li:first-child").css("color", "red");
    });
});

参考链接::first-child 选择器

英文:

Try this:

$(document).ready(function(){
$(&quot;#botao&quot;).click(function(){
$(&quot;ul li:first-child&quot;).css(&quot;color&quot;, &quot;red&quot;);
});
});

Reference: :first-child selector

huangapple
  • 本文由 发表于 2023年2月10日 04:55:28
  • 转载请务必保留本文链接:https://go.coder-hub.com/75404333.html
匿名

发表评论

匿名网友

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

确定