javascript - How to add a class to a span element which is part of an existing <li> tag with jQuery - Stack Overfl

How can I add a class to a span element for only these spans which are part of li tags with class='

How can I add a class to a span element for only these spans which are part of li tags with class='error' per example, this is the HTML which is generated:

<li class="error">
  <!--if list has class error, then add also class error to span which is part of that list tag-->
  <span class="error"></span>
</li>

This is the code I have so far:

if(status == 'error') {
  data.context.addClass('error'); // adds class error to li tag                 
}

How can I add a class to a span element for only these spans which are part of li tags with class='error' per example, this is the HTML which is generated:

<li class="error">
  <!--if list has class error, then add also class error to span which is part of that list tag-->
  <span class="error"></span>
</li>

This is the code I have so far:

if(status == 'error') {
  data.context.addClass('error'); // adds class error to li tag                 
}
Share Improve this question edited Feb 17, 2021 at 10:47 robd 9,8326 gold badges44 silver badges61 bronze badges asked Dec 24, 2018 at 17:05 Jack MaessenJack Maessen 1,8636 gold badges24 silver badges56 bronze badges
Add a ment  | 

5 Answers 5

Reset to default 2

This is easy using JQuery, just select all li items that have the class .error and then use find() to find all the spam elements inside it, finally add the class .error to those:

$(document).ready(function()
{
    $("li.error").find("span").addClass("error");
});
span.error {
    background-color: red;
}
<script src="https://cdnjs.cloudflare./ajax/libs/jquery/3.3.1/jquery.min.js"></script>

<li class="">
    <span>Parent "li" do not have class error</span>
</li>
<li class="error">
    <span>Parent "li" have class error</span>
</li>

This should achieve the expected result. Iterate through all li elements with the error class and find any spans, then add the class error to it.

$('li.error').each(function() {
    $(this).find('span').addClass('error');
})

For a pure vanilla JavaScript solution, you can use an attribute selector to find all your li elements that have a class called error. Once you have that list of li elements, just loop thru (using forEach or a for-loop) and append an a span element as a child of the li. The span element should also be given a class attribute called "error" via setAttribute.

function appendErrorSpan(li) {
  var errorEl = document.createElement('span');
  errorEl.setAttribute('class', 'error');
  errorEl.appendChild(document.createTextNode('Error span added!'));
  li.appendChild(errorEl);
};

var errorListItems = document.querySelectorAll('li[class="error"]');

errorListItems.forEach(appendErrorSpan);
<li class="">
  <span></span>
</li>
<li class="error">
</li>

Instead of using loops you can do it like this-

    let a=document.querySelectorAll("li.error > span");
    $(a).addClass('error');

Easiest way-

$("li.error > span").addClass('error');

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745375525a4624981.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信