如何从' group2'中删除空格? div span在页面加载时的类值。这是在页面加载时呈现的html。 span类值不应包含空格(例如:" ClassicalStudies"而不是" Classical Studies")。其他span类值也是如此。
<div class="group2">
<p class="subject-list">
<span class="Classical Studies">Classical Studies</span>
</p>
<p class="subject-list">
<span class="Cognitive Science">Cognitive Science</span>
</p>
<p class="subject-list">
<span class="Computer Engineering">Computer Engineering</span>
</p>
<p class="subject-list">
<span class="Computer Science">Computer Science</span>
</p>
答案 0 :(得分:1)
使用attr()
方法进行回调并替换class属性中的所有空格。
// attr method with callback would iterate over the
// elements internally and you can update value by returning
$('.group2 span').attr('class', function(i, c) {
// split by space and then join by empty string
return c.split(' ').join('');
// or replace all space by empty string
// return c.replace(/\s+/g,'');
})
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="group2">
<p class="subject-list">
<span class="Classical Studies">Classical Studies</span>
</p>
<p class="subject-list">
<span class="Cognitive Science">Cognitive Science</span>
</p>
<p class="subject-list">
<span class="Computer Engineering">Computer Engineering</span>
</p>
<p class="subject-list">
<span class="Computer Science">Computer Science</span>
</p>
&#13;